apply.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. "bytes"
  17. "fmt"
  18. "sort"
  19. "time"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/lease"
  22. "github.com/coreos/etcd/mvcc"
  23. "github.com/coreos/etcd/mvcc/mvccpb"
  24. "github.com/coreos/etcd/pkg/types"
  25. "github.com/gogo/protobuf/proto"
  26. "golang.org/x/net/context"
  27. )
  28. const (
  29. // noTxn is an invalid txn ID.
  30. // To apply with independent Range, Put, Delete, you can pass noTxn
  31. // to apply functions instead of a valid txn ID.
  32. noTxn = -1
  33. warnApplyDuration = 10 * time.Millisecond
  34. )
  35. type applyResult struct {
  36. resp proto.Message
  37. err error
  38. // physc signals the physical effect of the request has completed in addition
  39. // to being logically reflected by the node. Currently only used for
  40. // Compaction requests.
  41. physc <-chan struct{}
  42. }
  43. // applierV3 is the interface for processing V3 raft messages
  44. type applierV3 interface {
  45. Apply(r *pb.InternalRaftRequest) *applyResult
  46. Put(txnID int64, p *pb.PutRequest) (*pb.PutResponse, error)
  47. Range(txnID int64, r *pb.RangeRequest) (*pb.RangeResponse, error)
  48. DeleteRange(txnID int64, dr *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error)
  49. Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error)
  50. Compaction(compaction *pb.CompactionRequest) (*pb.CompactionResponse, <-chan struct{}, error)
  51. LeaseGrant(lc *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error)
  52. LeaseRevoke(lc *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error)
  53. Alarm(*pb.AlarmRequest) (*pb.AlarmResponse, error)
  54. Authenticate(r *pb.InternalAuthenticateRequest) (*pb.AuthenticateResponse, error)
  55. AuthEnable() (*pb.AuthEnableResponse, error)
  56. AuthDisable() (*pb.AuthDisableResponse, error)
  57. UserAdd(ua *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error)
  58. UserDelete(ua *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error)
  59. UserChangePassword(ua *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error)
  60. UserGrantRole(ua *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error)
  61. UserGet(ua *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error)
  62. UserRevokeRole(ua *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error)
  63. RoleAdd(ua *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error)
  64. RoleGrantPermission(ua *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error)
  65. RoleGet(ua *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error)
  66. RoleRevokePermission(ua *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error)
  67. RoleDelete(ua *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error)
  68. UserList(ua *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error)
  69. RoleList(ua *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error)
  70. }
  71. type applierV3backend struct {
  72. s *EtcdServer
  73. }
  74. func (s *EtcdServer) newApplierV3() applierV3 {
  75. return newAuthApplierV3(
  76. s.AuthStore(),
  77. newQuotaApplierV3(s, &applierV3backend{s}),
  78. )
  79. }
  80. func (a *applierV3backend) Apply(r *pb.InternalRaftRequest) *applyResult {
  81. ar := &applyResult{}
  82. // call into a.s.applyV3.F instead of a.F so upper appliers can check individual calls
  83. switch {
  84. case r.Range != nil:
  85. ar.resp, ar.err = a.s.applyV3.Range(noTxn, r.Range)
  86. case r.Put != nil:
  87. ar.resp, ar.err = a.s.applyV3.Put(noTxn, r.Put)
  88. case r.DeleteRange != nil:
  89. ar.resp, ar.err = a.s.applyV3.DeleteRange(noTxn, r.DeleteRange)
  90. case r.Txn != nil:
  91. ar.resp, ar.err = a.s.applyV3.Txn(r.Txn)
  92. case r.Compaction != nil:
  93. ar.resp, ar.physc, ar.err = a.s.applyV3.Compaction(r.Compaction)
  94. case r.LeaseGrant != nil:
  95. ar.resp, ar.err = a.s.applyV3.LeaseGrant(r.LeaseGrant)
  96. case r.LeaseRevoke != nil:
  97. ar.resp, ar.err = a.s.applyV3.LeaseRevoke(r.LeaseRevoke)
  98. case r.Alarm != nil:
  99. ar.resp, ar.err = a.s.applyV3.Alarm(r.Alarm)
  100. case r.Authenticate != nil:
  101. ar.resp, ar.err = a.s.applyV3.Authenticate(r.Authenticate)
  102. case r.AuthEnable != nil:
  103. ar.resp, ar.err = a.s.applyV3.AuthEnable()
  104. case r.AuthDisable != nil:
  105. ar.resp, ar.err = a.s.applyV3.AuthDisable()
  106. case r.AuthUserAdd != nil:
  107. ar.resp, ar.err = a.s.applyV3.UserAdd(r.AuthUserAdd)
  108. case r.AuthUserDelete != nil:
  109. ar.resp, ar.err = a.s.applyV3.UserDelete(r.AuthUserDelete)
  110. case r.AuthUserChangePassword != nil:
  111. ar.resp, ar.err = a.s.applyV3.UserChangePassword(r.AuthUserChangePassword)
  112. case r.AuthUserGrantRole != nil:
  113. ar.resp, ar.err = a.s.applyV3.UserGrantRole(r.AuthUserGrantRole)
  114. case r.AuthUserGet != nil:
  115. ar.resp, ar.err = a.s.applyV3.UserGet(r.AuthUserGet)
  116. case r.AuthUserRevokeRole != nil:
  117. ar.resp, ar.err = a.s.applyV3.UserRevokeRole(r.AuthUserRevokeRole)
  118. case r.AuthRoleAdd != nil:
  119. ar.resp, ar.err = a.s.applyV3.RoleAdd(r.AuthRoleAdd)
  120. case r.AuthRoleGrantPermission != nil:
  121. ar.resp, ar.err = a.s.applyV3.RoleGrantPermission(r.AuthRoleGrantPermission)
  122. case r.AuthRoleGet != nil:
  123. ar.resp, ar.err = a.s.applyV3.RoleGet(r.AuthRoleGet)
  124. case r.AuthRoleRevokePermission != nil:
  125. ar.resp, ar.err = a.s.applyV3.RoleRevokePermission(r.AuthRoleRevokePermission)
  126. case r.AuthRoleDelete != nil:
  127. ar.resp, ar.err = a.s.applyV3.RoleDelete(r.AuthRoleDelete)
  128. case r.AuthUserList != nil:
  129. ar.resp, ar.err = a.s.applyV3.UserList(r.AuthUserList)
  130. case r.AuthRoleList != nil:
  131. ar.resp, ar.err = a.s.applyV3.RoleList(r.AuthRoleList)
  132. default:
  133. panic("not implemented")
  134. }
  135. return ar
  136. }
  137. func (a *applierV3backend) Put(txnID int64, p *pb.PutRequest) (*pb.PutResponse, error) {
  138. resp := &pb.PutResponse{}
  139. resp.Header = &pb.ResponseHeader{}
  140. var (
  141. rev int64
  142. err error
  143. )
  144. var rr *mvcc.RangeResult
  145. if p.PrevKv {
  146. if txnID != noTxn {
  147. rr, err = a.s.KV().TxnRange(txnID, p.Key, nil, mvcc.RangeOptions{})
  148. if err != nil {
  149. return nil, err
  150. }
  151. } else {
  152. rr, err = a.s.KV().Range(p.Key, nil, mvcc.RangeOptions{})
  153. if err != nil {
  154. return nil, err
  155. }
  156. }
  157. }
  158. if txnID != noTxn {
  159. rev, err = a.s.KV().TxnPut(txnID, p.Key, p.Value, lease.LeaseID(p.Lease))
  160. if err != nil {
  161. return nil, err
  162. }
  163. } else {
  164. leaseID := lease.LeaseID(p.Lease)
  165. if leaseID != lease.NoLease {
  166. if l := a.s.lessor.Lookup(leaseID); l == nil {
  167. return nil, lease.ErrLeaseNotFound
  168. }
  169. }
  170. rev = a.s.KV().Put(p.Key, p.Value, leaseID)
  171. }
  172. resp.Header.Revision = rev
  173. if rr != nil && len(rr.KVs) != 0 {
  174. resp.PrevKv = &rr.KVs[0]
  175. }
  176. return resp, nil
  177. }
  178. func (a *applierV3backend) DeleteRange(txnID int64, dr *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  179. resp := &pb.DeleteRangeResponse{}
  180. resp.Header = &pb.ResponseHeader{}
  181. var (
  182. n int64
  183. rev int64
  184. err error
  185. )
  186. if isGteRange(dr.RangeEnd) {
  187. dr.RangeEnd = []byte{}
  188. }
  189. var rr *mvcc.RangeResult
  190. if dr.PrevKv {
  191. if txnID != noTxn {
  192. rr, err = a.s.KV().TxnRange(txnID, dr.Key, dr.RangeEnd, mvcc.RangeOptions{})
  193. if err != nil {
  194. return nil, err
  195. }
  196. } else {
  197. rr, err = a.s.KV().Range(dr.Key, dr.RangeEnd, mvcc.RangeOptions{})
  198. if err != nil {
  199. return nil, err
  200. }
  201. }
  202. }
  203. if txnID != noTxn {
  204. n, rev, err = a.s.KV().TxnDeleteRange(txnID, dr.Key, dr.RangeEnd)
  205. if err != nil {
  206. return nil, err
  207. }
  208. } else {
  209. n, rev = a.s.KV().DeleteRange(dr.Key, dr.RangeEnd)
  210. }
  211. resp.Deleted = n
  212. if rr != nil {
  213. for i := range rr.KVs {
  214. resp.PrevKvs = append(resp.PrevKvs, &rr.KVs[i])
  215. }
  216. }
  217. resp.Header.Revision = rev
  218. return resp, nil
  219. }
  220. func (a *applierV3backend) Range(txnID int64, r *pb.RangeRequest) (*pb.RangeResponse, error) {
  221. resp := &pb.RangeResponse{}
  222. resp.Header = &pb.ResponseHeader{}
  223. var (
  224. rr *mvcc.RangeResult
  225. err error
  226. )
  227. if isGteRange(r.RangeEnd) {
  228. r.RangeEnd = []byte{}
  229. }
  230. limit := r.Limit
  231. if r.SortOrder != pb.RangeRequest_NONE {
  232. // fetch everything; sort and truncate afterwards
  233. limit = 0
  234. }
  235. if limit > 0 {
  236. // fetch one extra for 'more' flag
  237. limit = limit + 1
  238. }
  239. ro := mvcc.RangeOptions{
  240. Limit: limit,
  241. Rev: r.Revision,
  242. Count: r.CountOnly,
  243. }
  244. if txnID != noTxn {
  245. rr, err = a.s.KV().TxnRange(txnID, r.Key, r.RangeEnd, ro)
  246. if err != nil {
  247. return nil, err
  248. }
  249. } else {
  250. rr, err = a.s.KV().Range(r.Key, r.RangeEnd, ro)
  251. if err != nil {
  252. return nil, err
  253. }
  254. }
  255. if r.SortOrder != pb.RangeRequest_NONE {
  256. var sorter sort.Interface
  257. switch {
  258. case r.SortTarget == pb.RangeRequest_KEY:
  259. sorter = &kvSortByKey{&kvSort{rr.KVs}}
  260. case r.SortTarget == pb.RangeRequest_VERSION:
  261. sorter = &kvSortByVersion{&kvSort{rr.KVs}}
  262. case r.SortTarget == pb.RangeRequest_CREATE:
  263. sorter = &kvSortByCreate{&kvSort{rr.KVs}}
  264. case r.SortTarget == pb.RangeRequest_MOD:
  265. sorter = &kvSortByMod{&kvSort{rr.KVs}}
  266. case r.SortTarget == pb.RangeRequest_VALUE:
  267. sorter = &kvSortByValue{&kvSort{rr.KVs}}
  268. }
  269. switch {
  270. case r.SortOrder == pb.RangeRequest_ASCEND:
  271. sort.Sort(sorter)
  272. case r.SortOrder == pb.RangeRequest_DESCEND:
  273. sort.Sort(sort.Reverse(sorter))
  274. }
  275. }
  276. if r.Limit > 0 && len(rr.KVs) > int(r.Limit) {
  277. rr.KVs = rr.KVs[:r.Limit]
  278. resp.More = true
  279. }
  280. resp.Header.Revision = rr.Rev
  281. resp.Count = int64(rr.Count)
  282. for i := range rr.KVs {
  283. if r.KeysOnly {
  284. rr.KVs[i].Value = nil
  285. }
  286. resp.Kvs = append(resp.Kvs, &rr.KVs[i])
  287. }
  288. return resp, nil
  289. }
  290. func (a *applierV3backend) Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error) {
  291. ok := true
  292. for _, c := range rt.Compare {
  293. if _, ok = a.applyCompare(c); !ok {
  294. break
  295. }
  296. }
  297. var reqs []*pb.RequestOp
  298. if ok {
  299. reqs = rt.Success
  300. } else {
  301. reqs = rt.Failure
  302. }
  303. if err := a.checkRequestLeases(reqs); err != nil {
  304. return nil, err
  305. }
  306. if err := a.checkRequestRange(reqs); err != nil {
  307. return nil, err
  308. }
  309. revision := a.s.KV().Rev()
  310. // When executing the operations of txn, we need to hold the txn lock.
  311. // So the reader will not see any intermediate results.
  312. txnID := a.s.KV().TxnBegin()
  313. defer func() {
  314. err := a.s.KV().TxnEnd(txnID)
  315. if err != nil {
  316. panic(fmt.Sprint("unexpected error when closing txn", txnID))
  317. }
  318. }()
  319. resps := make([]*pb.ResponseOp, len(reqs))
  320. changedKV := false
  321. for i := range reqs {
  322. if reqs[i].GetRequestRange() == nil {
  323. changedKV = true
  324. }
  325. resps[i] = a.applyUnion(txnID, reqs[i])
  326. }
  327. if changedKV {
  328. revision += 1
  329. }
  330. txnResp := &pb.TxnResponse{}
  331. txnResp.Header = &pb.ResponseHeader{}
  332. txnResp.Header.Revision = revision
  333. txnResp.Responses = resps
  334. txnResp.Succeeded = ok
  335. return txnResp, nil
  336. }
  337. // applyCompare applies the compare request.
  338. // It returns the revision at which the comparison happens. If the comparison
  339. // succeeds, the it returns true. Otherwise it returns false.
  340. func (a *applierV3backend) applyCompare(c *pb.Compare) (int64, bool) {
  341. rr, err := a.s.KV().Range(c.Key, nil, mvcc.RangeOptions{})
  342. rev := rr.Rev
  343. if err != nil {
  344. if err == mvcc.ErrTxnIDMismatch {
  345. panic("unexpected txn ID mismatch error")
  346. }
  347. return rev, false
  348. }
  349. var ckv mvccpb.KeyValue
  350. if len(rr.KVs) != 0 {
  351. ckv = rr.KVs[0]
  352. } else {
  353. // Use the zero value of ckv normally. However...
  354. if c.Target == pb.Compare_VALUE {
  355. // Always fail if we're comparing a value on a key that doesn't exist.
  356. // We can treat non-existence as the empty set explicitly, such that
  357. // even a key with a value of length 0 bytes is still a real key
  358. // that was written that way
  359. return rev, false
  360. }
  361. }
  362. // -1 is less, 0 is equal, 1 is greater
  363. var result int
  364. switch c.Target {
  365. case pb.Compare_VALUE:
  366. tv, _ := c.TargetUnion.(*pb.Compare_Value)
  367. if tv != nil {
  368. result = bytes.Compare(ckv.Value, tv.Value)
  369. }
  370. case pb.Compare_CREATE:
  371. tv, _ := c.TargetUnion.(*pb.Compare_CreateRevision)
  372. if tv != nil {
  373. result = compareInt64(ckv.CreateRevision, tv.CreateRevision)
  374. }
  375. case pb.Compare_MOD:
  376. tv, _ := c.TargetUnion.(*pb.Compare_ModRevision)
  377. if tv != nil {
  378. result = compareInt64(ckv.ModRevision, tv.ModRevision)
  379. }
  380. case pb.Compare_VERSION:
  381. tv, _ := c.TargetUnion.(*pb.Compare_Version)
  382. if tv != nil {
  383. result = compareInt64(ckv.Version, tv.Version)
  384. }
  385. }
  386. switch c.Result {
  387. case pb.Compare_EQUAL:
  388. if result != 0 {
  389. return rev, false
  390. }
  391. case pb.Compare_GREATER:
  392. if result != 1 {
  393. return rev, false
  394. }
  395. case pb.Compare_LESS:
  396. if result != -1 {
  397. return rev, false
  398. }
  399. }
  400. return rev, true
  401. }
  402. func (a *applierV3backend) applyUnion(txnID int64, union *pb.RequestOp) *pb.ResponseOp {
  403. switch tv := union.Request.(type) {
  404. case *pb.RequestOp_RequestRange:
  405. if tv.RequestRange != nil {
  406. resp, err := a.Range(txnID, tv.RequestRange)
  407. if err != nil {
  408. panic("unexpected error during txn")
  409. }
  410. return &pb.ResponseOp{Response: &pb.ResponseOp_ResponseRange{ResponseRange: resp}}
  411. }
  412. case *pb.RequestOp_RequestPut:
  413. if tv.RequestPut != nil {
  414. resp, err := a.Put(txnID, tv.RequestPut)
  415. if err != nil {
  416. panic("unexpected error during txn")
  417. }
  418. return &pb.ResponseOp{Response: &pb.ResponseOp_ResponsePut{ResponsePut: resp}}
  419. }
  420. case *pb.RequestOp_RequestDeleteRange:
  421. if tv.RequestDeleteRange != nil {
  422. resp, err := a.DeleteRange(txnID, tv.RequestDeleteRange)
  423. if err != nil {
  424. panic("unexpected error during txn")
  425. }
  426. return &pb.ResponseOp{Response: &pb.ResponseOp_ResponseDeleteRange{ResponseDeleteRange: resp}}
  427. }
  428. default:
  429. // empty union
  430. return nil
  431. }
  432. return nil
  433. }
  434. func (a *applierV3backend) Compaction(compaction *pb.CompactionRequest) (*pb.CompactionResponse, <-chan struct{}, error) {
  435. resp := &pb.CompactionResponse{}
  436. resp.Header = &pb.ResponseHeader{}
  437. ch, err := a.s.KV().Compact(compaction.Revision)
  438. if err != nil {
  439. return nil, ch, err
  440. }
  441. // get the current revision. which key to get is not important.
  442. rr, _ := a.s.KV().Range([]byte("compaction"), nil, mvcc.RangeOptions{})
  443. resp.Header.Revision = rr.Rev
  444. return resp, ch, err
  445. }
  446. func (a *applierV3backend) LeaseGrant(lc *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error) {
  447. l, err := a.s.lessor.Grant(lease.LeaseID(lc.ID), lc.TTL)
  448. resp := &pb.LeaseGrantResponse{}
  449. if err == nil {
  450. resp.ID = int64(l.ID)
  451. resp.TTL = l.TTL
  452. resp.Header = &pb.ResponseHeader{Revision: a.s.KV().Rev()}
  453. }
  454. return resp, err
  455. }
  456. func (a *applierV3backend) LeaseRevoke(lc *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error) {
  457. err := a.s.lessor.Revoke(lease.LeaseID(lc.ID))
  458. return &pb.LeaseRevokeResponse{Header: &pb.ResponseHeader{Revision: a.s.KV().Rev()}}, err
  459. }
  460. func (a *applierV3backend) Alarm(ar *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  461. resp := &pb.AlarmResponse{}
  462. oldCount := len(a.s.alarmStore.Get(ar.Alarm))
  463. switch ar.Action {
  464. case pb.AlarmRequest_GET:
  465. resp.Alarms = a.s.alarmStore.Get(ar.Alarm)
  466. case pb.AlarmRequest_ACTIVATE:
  467. m := a.s.alarmStore.Activate(types.ID(ar.MemberID), ar.Alarm)
  468. if m == nil {
  469. break
  470. }
  471. resp.Alarms = append(resp.Alarms, m)
  472. activated := oldCount == 0 && len(a.s.alarmStore.Get(m.Alarm)) == 1
  473. if !activated {
  474. break
  475. }
  476. switch m.Alarm {
  477. case pb.AlarmType_NOSPACE:
  478. plog.Warningf("alarm raised %+v", m)
  479. a.s.applyV3 = newApplierV3Capped(a)
  480. default:
  481. plog.Errorf("unimplemented alarm activation (%+v)", m)
  482. }
  483. case pb.AlarmRequest_DEACTIVATE:
  484. m := a.s.alarmStore.Deactivate(types.ID(ar.MemberID), ar.Alarm)
  485. if m == nil {
  486. break
  487. }
  488. resp.Alarms = append(resp.Alarms, m)
  489. deactivated := oldCount > 0 && len(a.s.alarmStore.Get(ar.Alarm)) == 0
  490. if !deactivated {
  491. break
  492. }
  493. switch m.Alarm {
  494. case pb.AlarmType_NOSPACE:
  495. plog.Infof("alarm disarmed %+v", ar)
  496. a.s.applyV3 = a.s.newApplierV3()
  497. default:
  498. plog.Errorf("unimplemented alarm deactivation (%+v)", m)
  499. }
  500. default:
  501. return nil, nil
  502. }
  503. return resp, nil
  504. }
  505. type applierV3Capped struct {
  506. applierV3
  507. q backendQuota
  508. }
  509. // newApplierV3Capped creates an applyV3 that will reject Puts and transactions
  510. // with Puts so that the number of keys in the store is capped.
  511. func newApplierV3Capped(base applierV3) applierV3 { return &applierV3Capped{applierV3: base} }
  512. func (a *applierV3Capped) Put(txnID int64, p *pb.PutRequest) (*pb.PutResponse, error) {
  513. return nil, ErrNoSpace
  514. }
  515. func (a *applierV3Capped) Txn(r *pb.TxnRequest) (*pb.TxnResponse, error) {
  516. if a.q.Cost(r) > 0 {
  517. return nil, ErrNoSpace
  518. }
  519. return a.applierV3.Txn(r)
  520. }
  521. func (a *applierV3Capped) LeaseGrant(lc *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error) {
  522. return nil, ErrNoSpace
  523. }
  524. func (a *applierV3backend) AuthEnable() (*pb.AuthEnableResponse, error) {
  525. err := a.s.AuthStore().AuthEnable()
  526. if err != nil {
  527. return nil, err
  528. }
  529. return &pb.AuthEnableResponse{}, nil
  530. }
  531. func (a *applierV3backend) AuthDisable() (*pb.AuthDisableResponse, error) {
  532. a.s.AuthStore().AuthDisable()
  533. return &pb.AuthDisableResponse{}, nil
  534. }
  535. func (a *applierV3backend) Authenticate(r *pb.InternalAuthenticateRequest) (*pb.AuthenticateResponse, error) {
  536. ctx := context.WithValue(context.WithValue(context.TODO(), "index", a.s.consistIndex.ConsistentIndex()), "simpleToken", r.SimpleToken)
  537. return a.s.AuthStore().Authenticate(ctx, r.Name, r.Password)
  538. }
  539. func (a *applierV3backend) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error) {
  540. return a.s.AuthStore().UserAdd(r)
  541. }
  542. func (a *applierV3backend) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) {
  543. return a.s.AuthStore().UserDelete(r)
  544. }
  545. func (a *applierV3backend) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error) {
  546. return a.s.AuthStore().UserChangePassword(r)
  547. }
  548. func (a *applierV3backend) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error) {
  549. return a.s.AuthStore().UserGrantRole(r)
  550. }
  551. func (a *applierV3backend) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
  552. return a.s.AuthStore().UserGet(r)
  553. }
  554. func (a *applierV3backend) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) {
  555. return a.s.AuthStore().UserRevokeRole(r)
  556. }
  557. func (a *applierV3backend) RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error) {
  558. return a.s.AuthStore().RoleAdd(r)
  559. }
  560. func (a *applierV3backend) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error) {
  561. return a.s.AuthStore().RoleGrantPermission(r)
  562. }
  563. func (a *applierV3backend) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error) {
  564. return a.s.AuthStore().RoleGet(r)
  565. }
  566. func (a *applierV3backend) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error) {
  567. return a.s.AuthStore().RoleRevokePermission(r)
  568. }
  569. func (a *applierV3backend) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error) {
  570. return a.s.AuthStore().RoleDelete(r)
  571. }
  572. func (a *applierV3backend) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error) {
  573. return a.s.AuthStore().UserList(r)
  574. }
  575. func (a *applierV3backend) RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error) {
  576. return a.s.AuthStore().RoleList(r)
  577. }
  578. type quotaApplierV3 struct {
  579. applierV3
  580. q Quota
  581. }
  582. func newQuotaApplierV3(s *EtcdServer, app applierV3) applierV3 {
  583. return &quotaApplierV3{app, NewBackendQuota(s)}
  584. }
  585. func (a *quotaApplierV3) Put(txnID int64, p *pb.PutRequest) (*pb.PutResponse, error) {
  586. ok := a.q.Available(p)
  587. resp, err := a.applierV3.Put(txnID, p)
  588. if err == nil && !ok {
  589. err = ErrNoSpace
  590. }
  591. return resp, err
  592. }
  593. func (a *quotaApplierV3) Txn(rt *pb.TxnRequest) (*pb.TxnResponse, error) {
  594. ok := a.q.Available(rt)
  595. resp, err := a.applierV3.Txn(rt)
  596. if err == nil && !ok {
  597. err = ErrNoSpace
  598. }
  599. return resp, err
  600. }
  601. func (a *quotaApplierV3) LeaseGrant(lc *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error) {
  602. ok := a.q.Available(lc)
  603. resp, err := a.applierV3.LeaseGrant(lc)
  604. if err == nil && !ok {
  605. err = ErrNoSpace
  606. }
  607. return resp, err
  608. }
  609. type kvSort struct{ kvs []mvccpb.KeyValue }
  610. func (s *kvSort) Swap(i, j int) {
  611. t := s.kvs[i]
  612. s.kvs[i] = s.kvs[j]
  613. s.kvs[j] = t
  614. }
  615. func (s *kvSort) Len() int { return len(s.kvs) }
  616. type kvSortByKey struct{ *kvSort }
  617. func (s *kvSortByKey) Less(i, j int) bool {
  618. return bytes.Compare(s.kvs[i].Key, s.kvs[j].Key) < 0
  619. }
  620. type kvSortByVersion struct{ *kvSort }
  621. func (s *kvSortByVersion) Less(i, j int) bool {
  622. return (s.kvs[i].Version - s.kvs[j].Version) < 0
  623. }
  624. type kvSortByCreate struct{ *kvSort }
  625. func (s *kvSortByCreate) Less(i, j int) bool {
  626. return (s.kvs[i].CreateRevision - s.kvs[j].CreateRevision) < 0
  627. }
  628. type kvSortByMod struct{ *kvSort }
  629. func (s *kvSortByMod) Less(i, j int) bool {
  630. return (s.kvs[i].ModRevision - s.kvs[j].ModRevision) < 0
  631. }
  632. type kvSortByValue struct{ *kvSort }
  633. func (s *kvSortByValue) Less(i, j int) bool {
  634. return bytes.Compare(s.kvs[i].Value, s.kvs[j].Value) < 0
  635. }
  636. func (a *applierV3backend) checkRequestLeases(reqs []*pb.RequestOp) error {
  637. for _, requ := range reqs {
  638. tv, ok := requ.Request.(*pb.RequestOp_RequestPut)
  639. if !ok {
  640. continue
  641. }
  642. preq := tv.RequestPut
  643. if preq == nil || lease.LeaseID(preq.Lease) == lease.NoLease {
  644. continue
  645. }
  646. if l := a.s.lessor.Lookup(lease.LeaseID(preq.Lease)); l == nil {
  647. return lease.ErrLeaseNotFound
  648. }
  649. }
  650. return nil
  651. }
  652. func (a *applierV3backend) checkRequestRange(reqs []*pb.RequestOp) error {
  653. for _, requ := range reqs {
  654. tv, ok := requ.Request.(*pb.RequestOp_RequestRange)
  655. if !ok {
  656. continue
  657. }
  658. greq := tv.RequestRange
  659. if greq == nil || greq.Revision == 0 {
  660. continue
  661. }
  662. if greq.Revision > a.s.KV().Rev() {
  663. return mvcc.ErrFutureRev
  664. }
  665. if greq.Revision < a.s.KV().FirstRev() {
  666. return mvcc.ErrCompacted
  667. }
  668. }
  669. return nil
  670. }
  671. func compareInt64(a, b int64) int {
  672. switch {
  673. case a < b:
  674. return -1
  675. case a > b:
  676. return 1
  677. default:
  678. return 0
  679. }
  680. }
  681. // isGteRange determines if the range end is a >= range. This works around grpc
  682. // sending empty byte strings as nil; >= is encoded in the range end as '\0'.
  683. func isGteRange(rangeEnd []byte) bool {
  684. return len(rangeEnd) == 1 && rangeEnd[0] == 0
  685. }