v3_server.go 22 KB

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