Beispiel #1
0
func (slv *slave) SendAuthToMaster() error {
	slv.mtx.Lock()
	var cli = slv.cli
	slv.mtx.Unlock()
	if cli == nil {
		return nil
	}
	if len(slv.adminPwd) == 0 {
		return nil
	}

	var p proto.PkgOneOp
	p.DbId = proto.AdminDbId
	p.Cmd = proto.CmdAuth
	p.RowKey = []byte(slv.adminPwd)

	var pkg = make([]byte, p.Length())
	_, err := p.Encode(pkg)
	if err != nil {
		return err
	}

	cli.AddResp(pkg)
	return nil
}
Beispiel #2
0
func (ms *master) syncStatus(key string, lastSeq uint64) {
	var p proto.PkgOneOp
	p.Cmd = proto.CmdSyncSt
	p.DbId = proto.AdminDbId
	p.Seq = lastSeq
	p.RowKey = []byte(key)
	var pkg = make([]byte, p.Length())
	p.Encode(pkg)
	ms.cli.AddResp(pkg)
}
Beispiel #3
0
func (srv *Server) syncStatus(req *Request) {
	var cliType uint32 = ClientTypeNormal
	if req.Cli != nil {
		cliType = req.Cli.ClientType()
	}
	switch cliType {
	case ClientTypeSlave:
		var in proto.PkgOneOp
		_, err := in.Decode(req.Pkg)
		if err != nil || len(in.RowKey) == 0 {
			return
		}

		rowKey := string(in.RowKey)
		switch rowKey {
		case store.KeyFullSyncEnd:
			srv.mc.SetStatus(ctrl.SlaveIncrSync)
			log.Printf("Switch sync status to SlaveIncrSync\n")
		case store.KeyIncrSyncEnd:
			var st = srv.mc.Status()
			srv.mc.SetStatus(ctrl.SlaveReady)

			var now = time.Now()
			srv.rwMtx.Lock()
			var lastTime = srv.readyTime
			srv.readyTime = now
			srv.rwMtx.Unlock()

			if st != ctrl.SlaveReady || now.Sub(lastTime).Seconds() > 120 {
				log.Printf("Switch sync status to SlaveReady")
			}
		case store.KeySyncLogMissing:
			srv.mc.SetStatus(ctrl.SlaveNeedClear)
			lastSeq, _ := srv.bin.GetMasterSeq()
			// Any better solution?
			log.Fatalf("Slave lastSeq %d is out of sync, please clear old data! "+
				"(Restart may fix this issue)", lastSeq)
		}

		if req.Seq > 0 {
			in.RowKey = nil // Set it as an empty OP
			req.Pkg = make([]byte, in.Length())
			in.Encode(req.Pkg)
			srv.sendResp(true, req, nil)
		}
	case ClientTypeNormal:
		log.Printf("User cannot send SYNCST command\n")
	case ClientTypeMaster:
		log.Printf("Slave SYNCST failed: [%d, %d]\n", req.DbId, req.Seq)
		req.Cli.Close()
	}
}
Beispiel #4
0
// Get, Set, Del, Incr, ZGet, ZSet, ZDel, ZIncr
func (c *Context) goOneOp(zop bool, cmd, tableId uint8,
	rowKey, colKey, value []byte, score int64, cas uint32,
	done chan *Call) (*Call, error) {
	call := c.cli.newCall(cmd, done)
	if call.err != nil {
		return call, call.err
	}

	var p proto.PkgOneOp
	p.Seq = call.seq
	p.DbId = c.dbId
	p.Cmd = call.cmd
	p.TableId = tableId
	p.RowKey = rowKey
	p.ColKey = colKey

	p.SetCas(cas)
	p.SetScore(score)
	p.SetValue(value)

	// ZGet, ZSet, ZDel, ZIncr
	if zop {
		p.PkgFlag |= proto.FlagZop
	}

	var pkgLen = p.Length()
	if pkgLen > proto.MaxPkgLen {
		c.cli.errCall(call, ErrInvPkgLen)
		return call, call.err
	}

	call.pkg = make([]byte, pkgLen)
	_, err := p.Encode(call.pkg)
	if err != nil {
		c.cli.errCall(call, err)
		return call, err
	}

	// put request pkg to sending channel
	c.cli.sending <- call

	return call, nil
}