v3_server.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // Copyright 2015 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. "context"
  18. "encoding/binary"
  19. "time"
  20. "go.etcd.io/etcd/auth"
  21. "go.etcd.io/etcd/etcdserver/api/membership"
  22. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  23. "go.etcd.io/etcd/lease"
  24. "go.etcd.io/etcd/lease/leasehttp"
  25. "go.etcd.io/etcd/mvcc"
  26. "go.etcd.io/etcd/pkg/traceutil"
  27. "go.etcd.io/etcd/raft"
  28. "github.com/gogo/protobuf/proto"
  29. "go.uber.org/zap"
  30. )
  31. const (
  32. // In the health case, there might be a small gap (10s of entries) between
  33. // the applied index and committed index.
  34. // However, if the committed entries are very heavy to apply, the gap might grow.
  35. // We should stop accepting new proposals if the gap growing to a certain point.
  36. maxGapBetweenApplyAndCommitIndex = 5000
  37. traceThreshold = 100 * time.Millisecond
  38. )
  39. type RaftKV interface {
  40. Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error)
  41. Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error)
  42. DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error)
  43. Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error)
  44. Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error)
  45. }
  46. type Lessor interface {
  47. // LeaseGrant sends LeaseGrant request to raft and apply it after committed.
  48. LeaseGrant(ctx context.Context, r *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error)
  49. // LeaseRevoke sends LeaseRevoke request to raft and apply it after committed.
  50. LeaseRevoke(ctx context.Context, r *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error)
  51. // LeaseRenew renews the lease with given ID. The renewed TTL is returned. Or an error
  52. // is returned.
  53. LeaseRenew(ctx context.Context, id lease.LeaseID) (int64, error)
  54. // LeaseTimeToLive retrieves lease information.
  55. LeaseTimeToLive(ctx context.Context, r *pb.LeaseTimeToLiveRequest) (*pb.LeaseTimeToLiveResponse, error)
  56. // LeaseLeases lists all leases.
  57. LeaseLeases(ctx context.Context, r *pb.LeaseLeasesRequest) (*pb.LeaseLeasesResponse, error)
  58. }
  59. type Authenticator interface {
  60. AuthEnable(ctx context.Context, r *pb.AuthEnableRequest) (*pb.AuthEnableResponse, error)
  61. AuthDisable(ctx context.Context, r *pb.AuthDisableRequest) (*pb.AuthDisableResponse, error)
  62. Authenticate(ctx context.Context, r *pb.AuthenticateRequest) (*pb.AuthenticateResponse, error)
  63. UserAdd(ctx context.Context, r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error)
  64. UserDelete(ctx context.Context, r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error)
  65. UserChangePassword(ctx context.Context, r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error)
  66. UserGrantRole(ctx context.Context, r *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error)
  67. UserGet(ctx context.Context, r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error)
  68. UserRevokeRole(ctx context.Context, r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error)
  69. RoleAdd(ctx context.Context, r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error)
  70. RoleGrantPermission(ctx context.Context, r *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error)
  71. RoleGet(ctx context.Context, r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error)
  72. RoleRevokePermission(ctx context.Context, r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error)
  73. RoleDelete(ctx context.Context, r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error)
  74. UserList(ctx context.Context, r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error)
  75. RoleList(ctx context.Context, r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error)
  76. }
  77. func (s *EtcdServer) Range(ctx context.Context, r *pb.RangeRequest) (*pb.RangeResponse, error) {
  78. trace := traceutil.New("range",
  79. s.getLogger(),
  80. traceutil.Field{Key: "range_begin", Value: string(r.Key)},
  81. traceutil.Field{Key: "range_end", Value: string(r.RangeEnd)},
  82. )
  83. ctx = context.WithValue(ctx, traceutil.TraceKey, trace)
  84. var resp *pb.RangeResponse
  85. var err error
  86. defer func(start time.Time) {
  87. warnOfExpensiveReadOnlyRangeRequest(s.getLogger(), start, r, resp, err)
  88. if resp != nil {
  89. trace.AddField(
  90. traceutil.Field{Key: "response_count", Value: len(resp.Kvs)},
  91. traceutil.Field{Key: "response_revision", Value: resp.Header.Revision},
  92. )
  93. }
  94. trace.LogIfLong(traceThreshold)
  95. }(time.Now())
  96. if !r.Serializable {
  97. err = s.linearizableReadNotify(ctx)
  98. trace.Step("agreement among raft nodes before linearized reading")
  99. if err != nil {
  100. return nil, err
  101. }
  102. }
  103. chk := func(ai *auth.AuthInfo) error {
  104. return s.authStore.IsRangePermitted(ai, r.Key, r.RangeEnd)
  105. }
  106. get := func() { resp, err = s.applyV3Base.Range(ctx, nil, r) }
  107. if serr := s.doSerialize(ctx, chk, get); serr != nil {
  108. err = serr
  109. return nil, err
  110. }
  111. return resp, err
  112. }
  113. func (s *EtcdServer) Put(ctx context.Context, r *pb.PutRequest) (*pb.PutResponse, error) {
  114. ctx = context.WithValue(ctx, traceutil.StartTimeKey, time.Now())
  115. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{Put: r})
  116. if err != nil {
  117. return nil, err
  118. }
  119. return resp.(*pb.PutResponse), nil
  120. }
  121. func (s *EtcdServer) DeleteRange(ctx context.Context, r *pb.DeleteRangeRequest) (*pb.DeleteRangeResponse, error) {
  122. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{DeleteRange: r})
  123. if err != nil {
  124. return nil, err
  125. }
  126. return resp.(*pb.DeleteRangeResponse), nil
  127. }
  128. func (s *EtcdServer) Txn(ctx context.Context, r *pb.TxnRequest) (*pb.TxnResponse, error) {
  129. if isTxnReadonly(r) {
  130. if !isTxnSerializable(r) {
  131. err := s.linearizableReadNotify(ctx)
  132. if err != nil {
  133. return nil, err
  134. }
  135. }
  136. var resp *pb.TxnResponse
  137. var err error
  138. chk := func(ai *auth.AuthInfo) error {
  139. return checkTxnAuth(s.authStore, ai, r)
  140. }
  141. defer func(start time.Time) {
  142. warnOfExpensiveReadOnlyTxnRequest(s.getLogger(), start, r, resp, err)
  143. }(time.Now())
  144. get := func() { resp, err = s.applyV3Base.Txn(r) }
  145. if serr := s.doSerialize(ctx, chk, get); serr != nil {
  146. return nil, serr
  147. }
  148. return resp, err
  149. }
  150. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{Txn: r})
  151. if err != nil {
  152. return nil, err
  153. }
  154. return resp.(*pb.TxnResponse), nil
  155. }
  156. func isTxnSerializable(r *pb.TxnRequest) bool {
  157. for _, u := range r.Success {
  158. if r := u.GetRequestRange(); r == nil || !r.Serializable {
  159. return false
  160. }
  161. }
  162. for _, u := range r.Failure {
  163. if r := u.GetRequestRange(); r == nil || !r.Serializable {
  164. return false
  165. }
  166. }
  167. return true
  168. }
  169. func isTxnReadonly(r *pb.TxnRequest) bool {
  170. for _, u := range r.Success {
  171. if r := u.GetRequestRange(); r == nil {
  172. return false
  173. }
  174. }
  175. for _, u := range r.Failure {
  176. if r := u.GetRequestRange(); r == nil {
  177. return false
  178. }
  179. }
  180. return true
  181. }
  182. func (s *EtcdServer) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
  183. startTime := time.Now()
  184. result, err := s.processInternalRaftRequestOnce(ctx, pb.InternalRaftRequest{Compaction: r})
  185. trace := traceutil.TODO()
  186. if result != nil && result.trace != nil {
  187. trace = result.trace
  188. defer func() {
  189. trace.LogIfLong(traceThreshold)
  190. }()
  191. applyStart := result.trace.GetStartTime()
  192. result.trace.SetStartTime(startTime)
  193. trace.InsertStep(0, applyStart, "process raft request")
  194. }
  195. if r.Physical && result != nil && result.physc != nil {
  196. <-result.physc
  197. // The compaction is done deleting keys; the hash is now settled
  198. // but the data is not necessarily committed. If there's a crash,
  199. // the hash may revert to a hash prior to compaction completing
  200. // if the compaction resumes. Force the finished compaction to
  201. // commit so it won't resume following a crash.
  202. s.be.ForceCommit()
  203. trace.Step("physically apply compaction")
  204. }
  205. if err != nil {
  206. return nil, err
  207. }
  208. if result.err != nil {
  209. return nil, result.err
  210. }
  211. resp := result.resp.(*pb.CompactionResponse)
  212. if resp == nil {
  213. resp = &pb.CompactionResponse{}
  214. }
  215. if resp.Header == nil {
  216. resp.Header = &pb.ResponseHeader{}
  217. }
  218. resp.Header.Revision = s.kv.Rev()
  219. trace.AddField(traceutil.Field{Key: "response_revision", Value: resp.Header.Revision})
  220. return resp, nil
  221. }
  222. func (s *EtcdServer) LeaseGrant(ctx context.Context, r *pb.LeaseGrantRequest) (*pb.LeaseGrantResponse, error) {
  223. // no id given? choose one
  224. for r.ID == int64(lease.NoLease) {
  225. // only use positive int64 id's
  226. r.ID = int64(s.reqIDGen.Next() & ((1 << 63) - 1))
  227. }
  228. resp, err := s.raftRequestOnce(ctx, pb.InternalRaftRequest{LeaseGrant: r})
  229. if err != nil {
  230. return nil, err
  231. }
  232. return resp.(*pb.LeaseGrantResponse), nil
  233. }
  234. func (s *EtcdServer) LeaseRevoke(ctx context.Context, r *pb.LeaseRevokeRequest) (*pb.LeaseRevokeResponse, error) {
  235. resp, err := s.raftRequestOnce(ctx, pb.InternalRaftRequest{LeaseRevoke: r})
  236. if err != nil {
  237. return nil, err
  238. }
  239. return resp.(*pb.LeaseRevokeResponse), nil
  240. }
  241. func (s *EtcdServer) LeaseRenew(ctx context.Context, id lease.LeaseID) (int64, error) {
  242. ttl, err := s.lessor.Renew(id)
  243. if err == nil { // already requested to primary lessor(leader)
  244. return ttl, nil
  245. }
  246. if err != lease.ErrNotPrimary {
  247. return -1, err
  248. }
  249. cctx, cancel := context.WithTimeout(ctx, s.Cfg.ReqTimeout())
  250. defer cancel()
  251. // renewals don't go through raft; forward to leader manually
  252. for cctx.Err() == nil && err != nil {
  253. leader, lerr := s.waitLeader(cctx)
  254. if lerr != nil {
  255. return -1, lerr
  256. }
  257. for _, url := range leader.PeerURLs {
  258. lurl := url + leasehttp.LeasePrefix
  259. ttl, err = leasehttp.RenewHTTP(cctx, id, lurl, s.peerRt)
  260. if err == nil || err == lease.ErrLeaseNotFound {
  261. return ttl, err
  262. }
  263. }
  264. }
  265. if cctx.Err() == context.DeadlineExceeded {
  266. return -1, ErrTimeout
  267. }
  268. return -1, ErrCanceled
  269. }
  270. func (s *EtcdServer) LeaseTimeToLive(ctx context.Context, r *pb.LeaseTimeToLiveRequest) (*pb.LeaseTimeToLiveResponse, error) {
  271. if s.Leader() == s.ID() {
  272. // primary; timetolive directly from leader
  273. le := s.lessor.Lookup(lease.LeaseID(r.ID))
  274. if le == nil {
  275. return nil, lease.ErrLeaseNotFound
  276. }
  277. // TODO: fill out ResponseHeader
  278. resp := &pb.LeaseTimeToLiveResponse{Header: &pb.ResponseHeader{}, ID: r.ID, TTL: int64(le.Remaining().Seconds()), GrantedTTL: le.TTL()}
  279. if r.Keys {
  280. ks := le.Keys()
  281. kbs := make([][]byte, len(ks))
  282. for i := range ks {
  283. kbs[i] = []byte(ks[i])
  284. }
  285. resp.Keys = kbs
  286. }
  287. return resp, nil
  288. }
  289. cctx, cancel := context.WithTimeout(ctx, s.Cfg.ReqTimeout())
  290. defer cancel()
  291. // forward to leader
  292. for cctx.Err() == nil {
  293. leader, err := s.waitLeader(cctx)
  294. if err != nil {
  295. return nil, err
  296. }
  297. for _, url := range leader.PeerURLs {
  298. lurl := url + leasehttp.LeaseInternalPrefix
  299. resp, err := leasehttp.TimeToLiveHTTP(cctx, lease.LeaseID(r.ID), r.Keys, lurl, s.peerRt)
  300. if err == nil {
  301. return resp.LeaseTimeToLiveResponse, nil
  302. }
  303. if err == lease.ErrLeaseNotFound {
  304. return nil, err
  305. }
  306. }
  307. }
  308. if cctx.Err() == context.DeadlineExceeded {
  309. return nil, ErrTimeout
  310. }
  311. return nil, ErrCanceled
  312. }
  313. func (s *EtcdServer) LeaseLeases(ctx context.Context, r *pb.LeaseLeasesRequest) (*pb.LeaseLeasesResponse, error) {
  314. ls := s.lessor.Leases()
  315. lss := make([]*pb.LeaseStatus, len(ls))
  316. for i := range ls {
  317. lss[i] = &pb.LeaseStatus{ID: int64(ls[i].ID)}
  318. }
  319. return &pb.LeaseLeasesResponse{Header: newHeader(s), Leases: lss}, nil
  320. }
  321. func (s *EtcdServer) waitLeader(ctx context.Context) (*membership.Member, error) {
  322. leader := s.cluster.Member(s.Leader())
  323. for leader == nil {
  324. // wait an election
  325. dur := time.Duration(s.Cfg.ElectionTicks) * time.Duration(s.Cfg.TickMs) * time.Millisecond
  326. select {
  327. case <-time.After(dur):
  328. leader = s.cluster.Member(s.Leader())
  329. case <-s.stopping:
  330. return nil, ErrStopped
  331. case <-ctx.Done():
  332. return nil, ErrNoLeader
  333. }
  334. }
  335. if leader == nil || len(leader.PeerURLs) == 0 {
  336. return nil, ErrNoLeader
  337. }
  338. return leader, nil
  339. }
  340. func (s *EtcdServer) Alarm(ctx context.Context, r *pb.AlarmRequest) (*pb.AlarmResponse, error) {
  341. resp, err := s.raftRequestOnce(ctx, pb.InternalRaftRequest{Alarm: r})
  342. if err != nil {
  343. return nil, err
  344. }
  345. return resp.(*pb.AlarmResponse), nil
  346. }
  347. func (s *EtcdServer) AuthEnable(ctx context.Context, r *pb.AuthEnableRequest) (*pb.AuthEnableResponse, error) {
  348. resp, err := s.raftRequestOnce(ctx, pb.InternalRaftRequest{AuthEnable: r})
  349. if err != nil {
  350. return nil, err
  351. }
  352. return resp.(*pb.AuthEnableResponse), nil
  353. }
  354. func (s *EtcdServer) AuthDisable(ctx context.Context, r *pb.AuthDisableRequest) (*pb.AuthDisableResponse, error) {
  355. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthDisable: r})
  356. if err != nil {
  357. return nil, err
  358. }
  359. return resp.(*pb.AuthDisableResponse), nil
  360. }
  361. func (s *EtcdServer) Authenticate(ctx context.Context, r *pb.AuthenticateRequest) (*pb.AuthenticateResponse, error) {
  362. if err := s.linearizableReadNotify(ctx); err != nil {
  363. return nil, err
  364. }
  365. lg := s.getLogger()
  366. var resp proto.Message
  367. for {
  368. checkedRevision, err := s.AuthStore().CheckPassword(r.Name, r.Password)
  369. if err != nil {
  370. if err != auth.ErrAuthNotEnabled {
  371. if lg != nil {
  372. lg.Warn(
  373. "invalid authentication was requested",
  374. zap.String("user", r.Name),
  375. zap.Error(err),
  376. )
  377. } else {
  378. plog.Errorf("invalid authentication request to user %s was issued", r.Name)
  379. }
  380. }
  381. return nil, err
  382. }
  383. st, err := s.AuthStore().GenTokenPrefix()
  384. if err != nil {
  385. return nil, err
  386. }
  387. internalReq := &pb.InternalAuthenticateRequest{
  388. Name: r.Name,
  389. Password: r.Password,
  390. SimpleToken: st,
  391. }
  392. resp, err = s.raftRequestOnce(ctx, pb.InternalRaftRequest{Authenticate: internalReq})
  393. if err != nil {
  394. return nil, err
  395. }
  396. if checkedRevision == s.AuthStore().Revision() {
  397. break
  398. }
  399. if lg != nil {
  400. lg.Info("revision when password checked became stale; retrying")
  401. } else {
  402. plog.Infof("revision when password checked is obsolete, retrying")
  403. }
  404. }
  405. return resp.(*pb.AuthenticateResponse), nil
  406. }
  407. func (s *EtcdServer) UserAdd(ctx context.Context, r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse, error) {
  408. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserAdd: r})
  409. if err != nil {
  410. return nil, err
  411. }
  412. return resp.(*pb.AuthUserAddResponse), nil
  413. }
  414. func (s *EtcdServer) UserDelete(ctx context.Context, r *pb.AuthUserDeleteRequest) (*pb.AuthUserDeleteResponse, error) {
  415. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserDelete: r})
  416. if err != nil {
  417. return nil, err
  418. }
  419. return resp.(*pb.AuthUserDeleteResponse), nil
  420. }
  421. func (s *EtcdServer) UserChangePassword(ctx context.Context, r *pb.AuthUserChangePasswordRequest) (*pb.AuthUserChangePasswordResponse, error) {
  422. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserChangePassword: r})
  423. if err != nil {
  424. return nil, err
  425. }
  426. return resp.(*pb.AuthUserChangePasswordResponse), nil
  427. }
  428. func (s *EtcdServer) UserGrantRole(ctx context.Context, r *pb.AuthUserGrantRoleRequest) (*pb.AuthUserGrantRoleResponse, error) {
  429. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserGrantRole: r})
  430. if err != nil {
  431. return nil, err
  432. }
  433. return resp.(*pb.AuthUserGrantRoleResponse), nil
  434. }
  435. func (s *EtcdServer) UserGet(ctx context.Context, r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) {
  436. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserGet: r})
  437. if err != nil {
  438. return nil, err
  439. }
  440. return resp.(*pb.AuthUserGetResponse), nil
  441. }
  442. func (s *EtcdServer) UserList(ctx context.Context, r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error) {
  443. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserList: r})
  444. if err != nil {
  445. return nil, err
  446. }
  447. return resp.(*pb.AuthUserListResponse), nil
  448. }
  449. func (s *EtcdServer) UserRevokeRole(ctx context.Context, r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUserRevokeRoleResponse, error) {
  450. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthUserRevokeRole: r})
  451. if err != nil {
  452. return nil, err
  453. }
  454. return resp.(*pb.AuthUserRevokeRoleResponse), nil
  455. }
  456. func (s *EtcdServer) RoleAdd(ctx context.Context, r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse, error) {
  457. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthRoleAdd: r})
  458. if err != nil {
  459. return nil, err
  460. }
  461. return resp.(*pb.AuthRoleAddResponse), nil
  462. }
  463. func (s *EtcdServer) RoleGrantPermission(ctx context.Context, r *pb.AuthRoleGrantPermissionRequest) (*pb.AuthRoleGrantPermissionResponse, error) {
  464. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthRoleGrantPermission: r})
  465. if err != nil {
  466. return nil, err
  467. }
  468. return resp.(*pb.AuthRoleGrantPermissionResponse), nil
  469. }
  470. func (s *EtcdServer) RoleGet(ctx context.Context, r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, error) {
  471. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthRoleGet: r})
  472. if err != nil {
  473. return nil, err
  474. }
  475. return resp.(*pb.AuthRoleGetResponse), nil
  476. }
  477. func (s *EtcdServer) RoleList(ctx context.Context, r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error) {
  478. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthRoleList: r})
  479. if err != nil {
  480. return nil, err
  481. }
  482. return resp.(*pb.AuthRoleListResponse), nil
  483. }
  484. func (s *EtcdServer) RoleRevokePermission(ctx context.Context, r *pb.AuthRoleRevokePermissionRequest) (*pb.AuthRoleRevokePermissionResponse, error) {
  485. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthRoleRevokePermission: r})
  486. if err != nil {
  487. return nil, err
  488. }
  489. return resp.(*pb.AuthRoleRevokePermissionResponse), nil
  490. }
  491. func (s *EtcdServer) RoleDelete(ctx context.Context, r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDeleteResponse, error) {
  492. resp, err := s.raftRequest(ctx, pb.InternalRaftRequest{AuthRoleDelete: r})
  493. if err != nil {
  494. return nil, err
  495. }
  496. return resp.(*pb.AuthRoleDeleteResponse), nil
  497. }
  498. func (s *EtcdServer) raftRequestOnce(ctx context.Context, r pb.InternalRaftRequest) (proto.Message, error) {
  499. result, err := s.processInternalRaftRequestOnce(ctx, r)
  500. if err != nil {
  501. return nil, err
  502. }
  503. if result.err != nil {
  504. return nil, result.err
  505. }
  506. if startTime, ok := ctx.Value(traceutil.StartTimeKey).(time.Time); ok && result.trace != nil {
  507. applyStart := result.trace.GetStartTime()
  508. // The trace object is created in apply. Here reset the start time to trace
  509. // the raft request time by the difference between the request start time
  510. // and apply start time
  511. result.trace.SetStartTime(startTime)
  512. result.trace.InsertStep(0, applyStart, "process raft request")
  513. result.trace.LogIfLong(traceThreshold)
  514. }
  515. return result.resp, nil
  516. }
  517. func (s *EtcdServer) raftRequest(ctx context.Context, r pb.InternalRaftRequest) (proto.Message, error) {
  518. for {
  519. resp, err := s.raftRequestOnce(ctx, r)
  520. if err != auth.ErrAuthOldRevision {
  521. return resp, err
  522. }
  523. }
  524. }
  525. // doSerialize handles the auth logic, with permissions checked by "chk", for a serialized request "get". Returns a non-nil error on authentication failure.
  526. func (s *EtcdServer) doSerialize(ctx context.Context, chk func(*auth.AuthInfo) error, get func()) error {
  527. trace := traceutil.Get(ctx)
  528. ai, err := s.AuthInfoFromCtx(ctx)
  529. if err != nil {
  530. return err
  531. }
  532. if ai == nil {
  533. // chk expects non-nil AuthInfo; use empty credentials
  534. ai = &auth.AuthInfo{}
  535. }
  536. if err = chk(ai); err != nil {
  537. return err
  538. }
  539. trace.Step("get authentication metadata")
  540. // fetch response for serialized request
  541. get()
  542. // check for stale token revision in case the auth store was updated while
  543. // the request has been handled.
  544. if ai.Revision != 0 && ai.Revision != s.authStore.Revision() {
  545. return auth.ErrAuthOldRevision
  546. }
  547. return nil
  548. }
  549. func (s *EtcdServer) processInternalRaftRequestOnce(ctx context.Context, r pb.InternalRaftRequest) (*applyResult, error) {
  550. ai := s.getAppliedIndex()
  551. ci := s.getCommittedIndex()
  552. if ci > ai+maxGapBetweenApplyAndCommitIndex {
  553. return nil, ErrTooManyRequests
  554. }
  555. r.Header = &pb.RequestHeader{
  556. ID: s.reqIDGen.Next(),
  557. }
  558. authInfo, err := s.AuthInfoFromCtx(ctx)
  559. if err != nil {
  560. return nil, err
  561. }
  562. if authInfo != nil {
  563. r.Header.Username = authInfo.Username
  564. r.Header.AuthRevision = authInfo.Revision
  565. }
  566. data, err := r.Marshal()
  567. if err != nil {
  568. return nil, err
  569. }
  570. if len(data) > int(s.Cfg.MaxRequestBytes) {
  571. return nil, ErrRequestTooLarge
  572. }
  573. id := r.ID
  574. if id == 0 {
  575. id = r.Header.ID
  576. }
  577. ch := s.w.Register(id)
  578. cctx, cancel := context.WithTimeout(ctx, s.Cfg.ReqTimeout())
  579. defer cancel()
  580. start := time.Now()
  581. err = s.r.Propose(cctx, data)
  582. if err != nil {
  583. proposalsFailed.Inc()
  584. s.w.Trigger(id, nil) // GC wait
  585. return nil, err
  586. }
  587. proposalsPending.Inc()
  588. defer proposalsPending.Dec()
  589. select {
  590. case x := <-ch:
  591. return x.(*applyResult), nil
  592. case <-cctx.Done():
  593. proposalsFailed.Inc()
  594. s.w.Trigger(id, nil) // GC wait
  595. return nil, s.parseProposeCtxErr(cctx.Err(), start)
  596. case <-s.done:
  597. return nil, ErrStopped
  598. }
  599. }
  600. // Watchable returns a watchable interface attached to the etcdserver.
  601. func (s *EtcdServer) Watchable() mvcc.WatchableKV { return s.KV() }
  602. func (s *EtcdServer) linearizableReadLoop() {
  603. var rs raft.ReadState
  604. for {
  605. ctxToSend := make([]byte, 8)
  606. id1 := s.reqIDGen.Next()
  607. binary.BigEndian.PutUint64(ctxToSend, id1)
  608. leaderChangedNotifier := s.leaderChangedNotify()
  609. select {
  610. case <-leaderChangedNotifier:
  611. continue
  612. case <-s.readwaitc:
  613. case <-s.stopping:
  614. return
  615. }
  616. nextnr := newNotifier()
  617. s.readMu.Lock()
  618. nr := s.readNotifier
  619. s.readNotifier = nextnr
  620. s.readMu.Unlock()
  621. lg := s.getLogger()
  622. cctx, cancel := context.WithTimeout(context.Background(), s.Cfg.ReqTimeout())
  623. if err := s.r.ReadIndex(cctx, ctxToSend); err != nil {
  624. cancel()
  625. if err == raft.ErrStopped {
  626. return
  627. }
  628. if lg != nil {
  629. lg.Warn("failed to get read index from Raft", zap.Error(err))
  630. } else {
  631. plog.Errorf("failed to get read index from raft: %v", err)
  632. }
  633. readIndexFailed.Inc()
  634. nr.notify(err)
  635. continue
  636. }
  637. cancel()
  638. var (
  639. timeout bool
  640. done bool
  641. )
  642. for !timeout && !done {
  643. select {
  644. case rs = <-s.r.readStateC:
  645. done = bytes.Equal(rs.RequestCtx, ctxToSend)
  646. if !done {
  647. // a previous request might time out. now we should ignore the response of it and
  648. // continue waiting for the response of the current requests.
  649. id2 := uint64(0)
  650. if len(rs.RequestCtx) == 8 {
  651. id2 = binary.BigEndian.Uint64(rs.RequestCtx)
  652. }
  653. if lg != nil {
  654. lg.Warn(
  655. "ignored out-of-date read index response; local node read indexes queueing up and waiting to be in sync with leader",
  656. zap.Uint64("sent-request-id", id1),
  657. zap.Uint64("received-request-id", id2),
  658. )
  659. } else {
  660. plog.Warningf("ignored out-of-date read index response; local node read indexes queueing up and waiting to be in sync with leader (request ID want %d, got %d)", id1, id2)
  661. }
  662. slowReadIndex.Inc()
  663. }
  664. case <-leaderChangedNotifier:
  665. timeout = true
  666. readIndexFailed.Inc()
  667. // return a retryable error.
  668. nr.notify(ErrLeaderChanged)
  669. case <-time.After(s.Cfg.ReqTimeout()):
  670. if lg != nil {
  671. lg.Warn("timed out waiting for read index response (local node might have slow network)", zap.Duration("timeout", s.Cfg.ReqTimeout()))
  672. } else {
  673. plog.Warningf("timed out waiting for read index response (local node might have slow network)")
  674. }
  675. nr.notify(ErrTimeout)
  676. timeout = true
  677. slowReadIndex.Inc()
  678. case <-s.stopping:
  679. return
  680. }
  681. }
  682. if !done {
  683. continue
  684. }
  685. if ai := s.getAppliedIndex(); ai < rs.Index {
  686. select {
  687. case <-s.applyWait.Wait(rs.Index):
  688. case <-s.stopping:
  689. return
  690. }
  691. }
  692. // unblock all l-reads requested at indices before rs.Index
  693. nr.notify(nil)
  694. }
  695. }
  696. func (s *EtcdServer) linearizableReadNotify(ctx context.Context) error {
  697. s.readMu.RLock()
  698. nc := s.readNotifier
  699. s.readMu.RUnlock()
  700. // signal linearizable loop for current notify if it hasn't been already
  701. select {
  702. case s.readwaitc <- struct{}{}:
  703. default:
  704. }
  705. // wait for read state notification
  706. select {
  707. case <-nc.c:
  708. return nc.err
  709. case <-ctx.Done():
  710. return ctx.Err()
  711. case <-s.done:
  712. return ErrStopped
  713. }
  714. }
  715. func (s *EtcdServer) AuthInfoFromCtx(ctx context.Context) (*auth.AuthInfo, error) {
  716. authInfo, err := s.AuthStore().AuthInfoFromCtx(ctx)
  717. if authInfo != nil || err != nil {
  718. return authInfo, err
  719. }
  720. if !s.Cfg.ClientCertAuthEnabled {
  721. return nil, nil
  722. }
  723. authInfo = s.AuthStore().AuthInfoFromTLS(ctx)
  724. return authInfo, nil
  725. }