Example #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
}
Example #2
0
func TestTableDel(t *testing.T) {
	var in proto.PkgOneOp
	in.Cmd = proto.CmdSet
	in.DbId = 1
	in.Seq = 10
	in.KeyValue = getTestKV(2, []byte("row1"), []byte("col1"), nil, 0, 0)

	myDel(in, testAuth, getTestWA(), true, t)
}
Example #3
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)
}
Example #4
0
func TestTableSetCas(t *testing.T) {
	var in proto.PkgOneOp
	in.Cmd = proto.CmdSet
	in.DbId = 1
	in.Seq = 10
	in.KeyValue = getTestKV(2, []byte("row1"), []byte("col1"), []byte("v1"), 30, 600)

	out := mySet(in, testAuth, getTestWA(), false, t)
	if out.ErrCode != table.EcCasNotMatch {
		t.Fatalf("Should fail with EcCasNotMatch")
	}
}
Example #5
0
func TestTableIncr(t *testing.T) {
	var in proto.PkgOneOp
	in.Cmd = proto.CmdGet
	in.DbId = 1
	in.Seq = 10
	in.KeyValue = getTestKV(2, []byte("row1"), []byte("col1"), nil, -21, 0)

	out := myIncr(in, testAuth, getTestWA(), true, t)
	if len(out.Value) != 0 {
		t.Fatalf("Value mismatch: %q", out.Value)
	}
	if out.Score != -21 {
		t.Fatalf("Score mismatch")
	}
}
Example #6
0
func TestTableGet(t *testing.T) {
	var in proto.PkgOneOp
	in.Cmd = proto.CmdGet
	in.DbId = 1
	in.Seq = 10
	in.KeyValue = getTestKV(2, []byte("row1"), []byte("col1"), nil, 0, 0)

	out := myGet(in, testAuth, getTestWA(), t)
	if bytes.Compare(out.Value, []byte("v1")) != 0 {
		t.Fatalf("Value mismatch: %q", out.Value)
	}
	if out.Score != 30 {
		t.Fatalf("Score mismatch")
	}
}
Example #7
0
func (tbl *Table) Auth(req *PkgArgs, au Authorize) []byte {
	var in proto.PkgOneOp
	_, err := in.Decode(req.Pkg)
	if err != nil {
		in.CtrlFlag &^= 0xFF // Clear all ctrl flags
		in.SetErrCode(table.EcDecodeFail)
		return replyHandle(&in)
	}

	in.ErrCode = 0
	var authDB uint8

	var already bool
	if au.IsAuth(proto.AdminDbId) {
		authDB = proto.AdminDbId
		already = true
	} else if in.DbId != proto.AdminDbId && au.IsAuth(in.DbId) {
		authDB = in.DbId
		already = true
	}
	if already {
		return replyHandle(&in)
	}

	password := string(in.RowKey)

	tbl.mtx.Lock()
	// Admin password
	if tbl.authPwd == nil || tbl.authPwd[proto.AdminDbId] == password {
		authDB = proto.AdminDbId
	} else {
		// Selected DB password
		if len(password) > 0 && tbl.authPwd[in.DbId] == password {
			authDB = in.DbId
		} else {
			in.SetErrCode(table.EcAuthFailed)
		}
	}
	tbl.mtx.Unlock()

	// Success
	if in.ErrCode == 0 {
		in.DbId = authDB
		au.SetAuth(authDB)
	}

	return replyHandle(&in)
}
Example #8
0
func (srv *Server) replyOneOp(req *Request, errCode int8) {
	var out proto.PkgOneOp
	out.Cmd = req.Cmd
	out.DbId = req.DbId
	out.Seq = req.Seq
	out.ErrCode = errCode
	if out.ErrCode != 0 {
		out.CtrlFlag |= proto.CtrlErrCode
	}

	var pkg = make([]byte, out.Length())
	_, err := out.Encode(pkg)
	if err != nil {
		log.Fatalf("Encode failed: %s\n", err)
	}

	srv.sendResp(false, req, pkg)
}
Example #9
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
}
Example #10
0
func TestTableGetCas(t *testing.T) {
	var in proto.PkgOneOp
	in.Cmd = proto.CmdGet
	in.DbId = 1
	in.Seq = 10
	in.KeyValue = getTestKV(2, []byte("row1"), []byte("col1"), []byte("v1"), 0, 2)

	out := myGet(in, testAuth, getTestWA(), t)
	if bytes.Compare(out.Value, []byte("v1")) != 0 {
		t.Fatalf("Value mismatch: %q", out.Value)
	}
	if out.Score != 30 {
		t.Fatalf("Score mismatch")
	}
	if out.Cas == 0 {
		t.Fatalf("Should return new cas")
	}

	// Set
	in.Cmd = proto.CmdSet
	in.SetValue(append(out.Value, []byte("-cas")...))
	in.SetScore(32)
	in.SetCas(out.Cas)

	mySet(in, testAuth, getTestWA(), true, t)

	// Set again should fail
	mySet(in, testAuth, getTestWA(), false, t)

	// Get
	in.Cmd = proto.CmdGet
	in.Cas = 0
	in.CtrlFlag &^= 0xFF

	out = myGet(in, testAuth, getTestWA(), t)
	if bytes.Compare(out.Value, []byte("v1-cas")) != 0 {
		t.Fatalf("Value mismatch: %q", out.Value)
	}
	if out.Score != 32 {
		t.Fatalf("Score mismatch")
	}
}
Example #11
0
func TestTableZopSetGet(t *testing.T) {
	var in proto.PkgOneOp
	in.PkgFlag |= proto.FlagZop
	in.Cmd = proto.CmdSet
	in.DbId = 1
	in.Seq = 10
	in.KeyValue = getTestKV(2, []byte("row1"), []byte("col1"), []byte("v1"), 30, 0)

	// ZSET
	mySet(in, testAuth, getTestWA(), true, t)

	// ZGET
	in.Cmd = proto.CmdGet
	out := myGet(in, testAuth, getTestWA(), t)

	if bytes.Compare(out.Value, []byte("v1")) != 0 {
		t.Fatalf("Value mismatch: %q", out.Value)
	}
	if out.Score != 30 {
		t.Fatalf("Score mismatch")
	}
}