Пример #1
0
func (b *replicationBlockset) Unmarshal(data []byte) error {
	r := bytes.NewReader(data)
	var rep int32
	err := binary.Read(r, binary.LittleEndian, &rep)
	if err != nil {
		return err
	}
	b.rep = int(rep)
	b.repBlocks = make([][]torus.BlockRef, rep)
	for rep := 0; rep < (b.rep - 1); rep++ {
		var l int32
		err := binary.Read(r, binary.LittleEndian, &l)
		if err != nil {
			return err
		}
		out := make([]torus.BlockRef, l)
		for i := 0; i < int(l); i++ {
			buf := make([]byte, torus.BlockRefByteSize)
			_, err := r.Read(buf)
			if err != nil {
				return err
			}
			out[i] = torus.BlockRefFromBytes(buf)
		}
		b.repBlocks[rep] = out
	}
	return nil
}
Пример #2
0
func (s *Server) handleRebalanceCheck(conn net.Conn, len int, refbuf []byte) error {
	refs := make([]torus.BlockRef, len)
	for i := 0; i < len; i++ {
		err := readConnIntoBuffer(conn, refbuf)
		if err != nil {
			return err
		}
		refs[i] = torus.BlockRefFromBytes(refbuf)
	}
	bools, err := s.handler.RebalanceCheck(context.TODO(), refs)
	respheader := headerOk
	if err != nil {
		clog.Warningf("failed to rebalance check: %v", err)
		respheader = headerErr
	}
	_, err = conn.Write(respheader)
	if err != nil {
		return err
	}
	bs := bitsetFromBool(bools)
	_, err = conn.Write(bs)
	if err != nil {
		return err
	}
	return nil
}
Пример #3
0
func (s *Server) handlePutBlock(conn net.Conn, refbuf []byte, null []byte) error {
	err := readConnIntoBuffer(conn, refbuf)
	if err != nil {
		return err
	}
	ref := torus.BlockRefFromBytes(refbuf)
	data, err := s.handler.WriteBuf(context.TODO(), ref)
	if err != nil {
		if err == torus.ErrExists {
			data = null
		} else {
			return err
		}
	}
	err = readConnIntoBuffer(conn, data)
	if err != nil {
		return err
	}
	respheader := headerOk
	if err != nil {
		respheader = headerErr
	}
	_, err = conn.Write(respheader)
	return err
}
func (b *baseBlockset) Unmarshal(data []byte) error {
	l := len(data) / torus.BlockRefByteSize
	out := make([]torus.BlockRef, l)
	for i := 0; i < l; i++ {
		out[i] = torus.BlockRefFromBytes(data[(i * torus.BlockRefByteSize) : (i+1)*torus.BlockRefByteSize])
	}
	b.blocks = out
	return nil
}
func (s *Server) handleBlock(conn net.Conn, refbuf []byte) error {
	err := readConnIntoBuffer(conn, refbuf)
	if err != nil {
		return err
	}
	ref := torus.BlockRefFromBytes(refbuf)
	data, err := s.handler.Block(context.TODO(), ref)
	respheader := headerOk
	if err != nil {
		respheader = headerErr
	}
	_, err = conn.Write(respheader)
	if err != nil {
		return err
	}
	_, err = conn.Write(data)
	if err != nil {
		return err
	}
	return nil
}
func loadIndex(m *MFile) (map[torus.BlockRef]int, error) {
	clog.Infof("loading block index...")
	var membefore uint64
	if clog.LevelAt(capnslog.DEBUG) {
		var mem runtime.MemStats
		runtime.ReadMemStats(&mem)
		membefore = mem.Alloc
	}
	out := make(map[torus.BlockRef]int)
	for i := uint64(0); i < m.NumBlocks(); i++ {
		b := m.GetBlock(i)
		if bytes.Equal(blankRefBytes, b) {
			continue
		}
		out[torus.BlockRefFromBytes(b)] = int(i)
	}
	if clog.LevelAt(capnslog.DEBUG) {
		var mem runtime.MemStats
		runtime.ReadMemStats(&mem)
		clog.Debugf("index memory usage: %dK", ((mem.Alloc - membefore) / 1024))
	}
	clog.Infof("done loading block index")
	return out, nil
}