├── .DS_Store ├── .gitignore ├── LICENSE ├── README.md ├── RedisGO ├── benchmark.sh ├── benchmark2.sh ├── cluster_config.json ├── cluster_config2.json ├── cluster_test ├── RedisGO ├── cluster_config.json ├── cluster_config1.json ├── cluster_config2.json ├── cluster_config3.json ├── cluster_test.sh ├── cluster_test_new_node_join.sh ├── redis.conf ├── redis1.conf ├── redis2.conf └── redis3.conf ├── config ├── config.go └── config_test.go ├── etcd ├── .gitignore ├── LICENSE ├── api │ ├── LICENSE │ ├── authpb │ │ ├── auth.pb.go │ │ └── auth.proto │ ├── etcdserverpb │ │ ├── etcdserver.pb.go │ │ ├── etcdserver.proto │ │ ├── gw │ │ │ └── rpc.pb.gw.go │ │ ├── raft_internal.pb.go │ │ ├── raft_internal.proto │ │ ├── raft_internal_stringer.go │ │ ├── raft_internal_stringer_test.go │ │ ├── rpc.pb.go │ │ └── rpc.proto │ ├── go.mod │ ├── go.sum │ ├── membershippb │ │ ├── membership.pb.go │ │ └── membership.proto │ ├── mvccpb │ │ ├── kv.pb.go │ │ └── kv.proto │ ├── v3rpc │ │ └── rpctypes │ │ │ ├── doc.go │ │ │ ├── error.go │ │ │ ├── error_test.go │ │ │ ├── md.go │ │ │ └── metadatafields.go │ ├── version │ │ ├── version.go │ │ └── version_test.go │ └── versionpb │ │ ├── version.pb.go │ │ └── version.proto ├── client │ ├── pkg │ │ ├── LICENSE │ │ ├── fileutil │ │ │ ├── dir_unix.go │ │ │ ├── dir_windows.go │ │ │ ├── doc.go │ │ │ ├── filereader.go │ │ │ ├── filereader_test.go │ │ │ ├── fileutil.go │ │ │ ├── fileutil_test.go │ │ │ ├── lock.go │ │ │ ├── lock_flock.go │ │ │ ├── lock_linux.go │ │ │ ├── lock_linux_test.go │ │ │ ├── lock_plan9.go │ │ │ ├── lock_solaris.go │ │ │ ├── lock_test.go │ │ │ ├── lock_unix.go │ │ │ ├── lock_windows.go │ │ │ ├── preallocate.go │ │ │ ├── preallocate_darwin.go │ │ │ ├── preallocate_test.go │ │ │ ├── preallocate_unix.go │ │ │ ├── preallocate_unsupported.go │ │ │ ├── purge.go │ │ │ ├── purge_test.go │ │ │ ├── read_dir.go │ │ │ ├── read_dir_test.go │ │ │ ├── sync.go │ │ │ ├── sync_darwin.go │ │ │ └── sync_linux.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── logutil │ │ │ ├── doc.go │ │ │ ├── log_format.go │ │ │ ├── log_format_test.go │ │ │ ├── log_level.go │ │ │ ├── zap.go │ │ │ ├── zap_journal.go │ │ │ └── zap_journal_test.go │ │ ├── pathutil │ │ │ ├── path.go │ │ │ └── path_test.go │ │ ├── srv │ │ │ ├── srv.go │ │ │ └── srv_test.go │ │ ├── systemd │ │ │ ├── doc.go │ │ │ └── journal.go │ │ ├── testutil │ │ │ ├── assert.go │ │ │ ├── before.go │ │ │ ├── leak.go │ │ │ ├── leak_test.go │ │ │ ├── pauseable_handler.go │ │ │ ├── recorder.go │ │ │ ├── testingtb.go │ │ │ ├── testutil.go │ │ │ └── var.go │ │ ├── tlsutil │ │ │ ├── cipher_suites.go │ │ │ ├── cipher_suites_test.go │ │ │ ├── doc.go │ │ │ └── tlsutil.go │ │ ├── transport │ │ │ ├── doc.go │ │ │ ├── keepalive_listener.go │ │ │ ├── keepalive_listener_test.go │ │ │ ├── limit_listen.go │ │ │ ├── listener.go │ │ │ ├── listener_opts.go │ │ │ ├── listener_test.go │ │ │ ├── listener_tls.go │ │ │ ├── sockopt.go │ │ │ ├── sockopt_solaris.go │ │ │ ├── sockopt_unix.go │ │ │ ├── sockopt_windows.go │ │ │ ├── timeout_conn.go │ │ │ ├── timeout_dialer.go │ │ │ ├── timeout_dialer_test.go │ │ │ ├── timeout_listener.go │ │ │ ├── timeout_listener_test.go │ │ │ ├── timeout_transport.go │ │ │ ├── timeout_transport_test.go │ │ │ ├── tls.go │ │ │ ├── tls_test.go │ │ │ ├── transport.go │ │ │ ├── transport_test.go │ │ │ └── unix_listener.go │ │ ├── types │ │ │ ├── doc.go │ │ │ ├── id.go │ │ │ ├── id_test.go │ │ │ ├── set.go │ │ │ ├── set_test.go │ │ │ ├── slice.go │ │ │ ├── slice_test.go │ │ │ ├── urls.go │ │ │ ├── urls_test.go │ │ │ ├── urlsmap.go │ │ │ └── urlsmap_test.go │ │ └── verify │ │ │ └── verify.go │ ├── v2 │ │ ├── LICENSE │ │ ├── README.md │ │ ├── auth_role.go │ │ ├── auth_user.go │ │ ├── cancelreq.go │ │ ├── client.go │ │ ├── client_test.go │ │ ├── cluster_error.go │ │ ├── curl.go │ │ ├── discover.go │ │ ├── doc.go │ │ ├── fake_transport_test.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── keys.go │ │ ├── keys_bench_test.go │ │ ├── keys_test.go │ │ ├── main_test.go │ │ ├── members.go │ │ ├── members_test.go │ │ └── util.go │ └── v3 │ │ ├── LICENSE │ │ ├── README.md │ │ ├── auth.go │ │ ├── client.go │ │ ├── client_test.go │ │ ├── clientv3util │ │ ├── example_key_test.go │ │ └── util.go │ │ ├── cluster.go │ │ ├── compact_op.go │ │ ├── compact_op_test.go │ │ ├── compare.go │ │ ├── concurrency │ │ ├── doc.go │ │ ├── election.go │ │ ├── example_election_test.go │ │ ├── example_mutex_test.go │ │ ├── example_stm_test.go │ │ ├── key.go │ │ ├── main_test.go │ │ ├── mutex.go │ │ ├── session.go │ │ └── stm.go │ │ ├── config.go │ │ ├── config_test.go │ │ ├── credentials │ │ └── credentials.go │ │ ├── ctx.go │ │ ├── ctx_test.go │ │ ├── doc.go │ │ ├── example_auth_test.go │ │ ├── example_cluster_test.go │ │ ├── example_kv_test.go │ │ ├── example_lease_test.go │ │ ├── example_maintenance_test.go │ │ ├── example_metrics_test.go │ │ ├── example_test.go │ │ ├── example_watch_test.go │ │ ├── experimental │ │ └── recipes │ │ │ ├── barrier.go │ │ │ ├── client.go │ │ │ ├── doc.go │ │ │ ├── double_barrier.go │ │ │ ├── grpc_gateway │ │ │ └── user_add.sh │ │ │ ├── key.go │ │ │ ├── priority_queue.go │ │ │ ├── queue.go │ │ │ ├── rwmutex.go │ │ │ └── watch.go │ │ ├── go.mod │ │ ├── go.sum │ │ ├── internal │ │ ├── endpoint │ │ │ ├── endpoint.go │ │ │ └── endpoint_test.go │ │ └── resolver │ │ │ └── resolver.go │ │ ├── kv.go │ │ ├── lease.go │ │ ├── leasing │ │ ├── cache.go │ │ ├── doc.go │ │ ├── kv.go │ │ ├── txn.go │ │ └── util.go │ │ ├── logger.go │ │ ├── main_test.go │ │ ├── maintenance.go │ │ ├── mirror │ │ └── syncer.go │ │ ├── mock │ │ └── mockserver │ │ │ ├── doc.go │ │ │ └── mockserver.go │ │ ├── namespace │ │ ├── doc.go │ │ ├── kv.go │ │ ├── lease.go │ │ ├── util.go │ │ ├── util_test.go │ │ └── watch.go │ │ ├── naming │ │ ├── doc.go │ │ ├── endpoints │ │ │ ├── endpoints.go │ │ │ ├── endpoints_impl.go │ │ │ └── internal │ │ │ │ └── update.go │ │ └── resolver │ │ │ └── resolver.go │ │ ├── op.go │ │ ├── op_test.go │ │ ├── options.go │ │ ├── ordering │ │ ├── doc.go │ │ ├── kv.go │ │ ├── kv_test.go │ │ └── util.go │ │ ├── retry.go │ │ ├── retry_interceptor.go │ │ ├── retry_interceptor_test.go │ │ ├── snapshot │ │ ├── doc.go │ │ └── v3_snapshot.go │ │ ├── sort.go │ │ ├── txn.go │ │ ├── txn_test.go │ │ ├── utils.go │ │ ├── watch.go │ │ ├── watch_test.go │ │ └── yaml │ │ ├── config.go │ │ └── config_test.go ├── go.mod ├── go.sum ├── pkg │ ├── LICENSE │ ├── README.md │ ├── adt │ │ ├── README.md │ │ ├── adt.go │ │ ├── example_test.go │ │ ├── img │ │ │ ├── red-black-tree-01-insertion.png │ │ │ ├── red-black-tree-02-delete-514.png │ │ │ ├── red-black-tree-03-delete-11.png │ │ │ ├── red-black-tree-04-delete-11.png │ │ │ ├── red-black-tree-05-delete-11.png │ │ │ ├── red-black-tree-06-delete-11.png │ │ │ ├── red-black-tree-07-delete-11.png │ │ │ ├── red-black-tree-08-delete-11.png │ │ │ └── red-black-tree-09-delete-11.png │ │ ├── interval_tree.go │ │ └── interval_tree_test.go │ ├── cobrautl │ │ ├── error.go │ │ └── help.go │ ├── contention │ │ ├── contention.go │ │ └── doc.go │ ├── cpuutil │ │ ├── doc.go │ │ └── endian.go │ ├── crc │ │ ├── crc.go │ │ └── crc_test.go │ ├── debugutil │ │ ├── doc.go │ │ └── pprof.go │ ├── expect │ │ ├── expect.go │ │ └── expect_test.go │ ├── flags │ │ ├── flag.go │ │ ├── flag_test.go │ │ ├── ignored.go │ │ ├── selective_string.go │ │ ├── selective_string_test.go │ │ ├── strings.go │ │ ├── strings_test.go │ │ ├── uint32.go │ │ ├── uint32_test.go │ │ ├── unique_strings.go │ │ ├── unique_strings_test.go │ │ ├── unique_urls.go │ │ ├── unique_urls_test.go │ │ ├── urls.go │ │ └── urls_test.go │ ├── go.mod │ ├── go.sum │ ├── grpc_testing │ │ ├── recorder.go │ │ └── stub_server.go │ ├── httputil │ │ ├── httputil.go │ │ └── httputil_test.go │ ├── idutil │ │ ├── id.go │ │ └── id_test.go │ ├── ioutil │ │ ├── pagewriter.go │ │ ├── pagewriter_test.go │ │ ├── readcloser.go │ │ ├── readcloser_test.go │ │ ├── reader.go │ │ ├── reader_test.go │ │ └── util.go │ ├── netutil │ │ ├── doc.go │ │ ├── netutil.go │ │ ├── netutil_test.go │ │ ├── routes.go │ │ ├── routes_linux.go │ │ └── routes_linux_test.go │ ├── notify │ │ └── notify.go │ ├── osutil │ │ ├── interrupt_unix.go │ │ ├── interrupt_windows.go │ │ ├── osutil.go │ │ ├── osutil_test.go │ │ ├── signal.go │ │ └── signal_linux.go │ ├── pbutil │ │ ├── pbutil.go │ │ └── pbutil_test.go │ ├── proxy │ │ ├── doc.go │ │ ├── fixtures │ │ │ ├── ca-csr.json │ │ │ ├── ca.crt │ │ │ ├── gencert.json │ │ │ ├── gencerts.sh │ │ │ ├── server-ca-csr.json │ │ │ ├── server.crt │ │ │ └── server.key.insecure │ │ ├── server.go │ │ └── server_test.go │ ├── report │ │ ├── doc.go │ │ ├── report.go │ │ ├── report_test.go │ │ ├── timeseries.go │ │ ├── timeseries_test.go │ │ └── weighted.go │ ├── runtime │ │ ├── fds_linux.go │ │ └── fds_other.go │ ├── schedule │ │ ├── doc.go │ │ ├── schedule.go │ │ └── schedule_test.go │ ├── stringutil │ │ ├── doc.go │ │ ├── rand.go │ │ └── rand_test.go │ ├── traceutil │ │ ├── trace.go │ │ └── trace_test.go │ └── wait │ │ ├── wait.go │ │ ├── wait_test.go │ │ ├── wait_time.go │ │ └── wait_time_test.go ├── raft │ ├── LICENSE │ ├── OWNERS │ ├── README.md │ ├── bootstrap.go │ ├── confchange │ │ ├── confchange.go │ │ ├── datadriven_test.go │ │ ├── quick_test.go │ │ ├── restore.go │ │ ├── restore_test.go │ │ └── testdata │ │ │ ├── joint_autoleave.txt │ │ │ ├── joint_idempotency.txt │ │ │ ├── joint_learners_next.txt │ │ │ ├── joint_safety.txt │ │ │ ├── simple_idempotency.txt │ │ │ ├── simple_promote_demote.txt │ │ │ ├── simple_safety.txt │ │ │ ├── update.txt │ │ │ └── zero.txt │ ├── design.md │ ├── diff_test.go │ ├── doc.go │ ├── example_test.go │ ├── go.mod │ ├── go.sum │ ├── interaction_test.go │ ├── log.go │ ├── log_test.go │ ├── log_unstable.go │ ├── log_unstable_test.go │ ├── logger.go │ ├── node.go │ ├── node_bench_test.go │ ├── node_test.go │ ├── node_util_test.go │ ├── quorum │ │ ├── bench_test.go │ │ ├── datadriven_test.go │ │ ├── joint.go │ │ ├── majority.go │ │ ├── quick_test.go │ │ ├── quorum.go │ │ ├── testdata │ │ │ ├── joint_commit.txt │ │ │ ├── joint_vote.txt │ │ │ ├── majority_commit.txt │ │ │ └── majority_vote.txt │ │ └── voteresult_string.go │ ├── raft.go │ ├── raft_flow_control_test.go │ ├── raft_paper_test.go │ ├── raft_snap_test.go │ ├── raft_test.go │ ├── raftpb │ │ ├── confchange.go │ │ ├── confstate.go │ │ ├── confstate_test.go │ │ ├── raft.pb.go │ │ ├── raft.proto │ │ └── raft_test.go │ ├── rafttest │ │ ├── doc.go │ │ ├── interaction_env.go │ │ ├── interaction_env_handler.go │ │ ├── interaction_env_handler_add_nodes.go │ │ ├── interaction_env_handler_campaign.go │ │ ├── interaction_env_handler_compact.go │ │ ├── interaction_env_handler_deliver_msgs.go │ │ ├── interaction_env_handler_log_level.go │ │ ├── interaction_env_handler_process_ready.go │ │ ├── interaction_env_handler_propose.go │ │ ├── interaction_env_handler_propose_conf_change.go │ │ ├── interaction_env_handler_raft_log.go │ │ ├── interaction_env_handler_raftstate.go │ │ ├── interaction_env_handler_stabilize.go │ │ ├── interaction_env_handler_status.go │ │ ├── interaction_env_handler_tick_heartbeat.go │ │ ├── interaction_env_handler_transfer_leadership.go │ │ ├── interaction_env_logger.go │ │ ├── network.go │ │ ├── network_test.go │ │ ├── node.go │ │ ├── node_bench_test.go │ │ └── node_test.go │ ├── rawnode.go │ ├── rawnode_test.go │ ├── read_only.go │ ├── status.go │ ├── storage.go │ ├── storage_test.go │ ├── testdata │ │ ├── campaign.txt │ │ ├── campaign_learner_must_vote.txt │ │ ├── confchange_v1_add_single.txt │ │ ├── confchange_v1_remove_leader.txt │ │ ├── confchange_v2_add_double_auto.txt │ │ ├── confchange_v2_add_double_implicit.txt │ │ ├── confchange_v2_add_single_auto.txt │ │ ├── confchange_v2_add_single_explicit.txt │ │ ├── confchange_v2_replace_leader.txt │ │ ├── probe_and_replicate.txt │ │ ├── single_node.txt │ │ └── snapshot_succeed_via_app_resp.txt │ ├── tracker │ │ ├── inflights.go │ │ ├── inflights_test.go │ │ ├── progress.go │ │ ├── progress_test.go │ │ ├── state.go │ │ └── tracker.go │ ├── util.go │ └── util_test.go └── server │ ├── LICENSE │ ├── auth │ ├── doc.go │ ├── jwt.go │ ├── jwt_test.go │ ├── main_test.go │ ├── metrics.go │ ├── nop.go │ ├── options.go │ ├── range_perm_cache.go │ ├── range_perm_cache_test.go │ ├── simple_token.go │ ├── simple_token_test.go │ ├── store.go │ ├── store_mock_test.go │ └── store_test.go │ ├── config │ ├── config.go │ ├── config_test.go │ ├── v2_deprecation.go │ └── v2_deprecation_test.go │ ├── embed │ ├── auth_test.go │ ├── config.go │ ├── config_logging.go │ ├── config_logging_journal_unix.go │ ├── config_logging_journal_windows.go │ ├── config_test.go │ ├── config_tracing.go │ ├── config_tracing_test.go │ ├── doc.go │ ├── etcd.go │ ├── serve.go │ ├── serve_test.go │ └── util.go │ ├── etcdmain │ ├── config.go │ ├── config_test.go │ ├── doc.go │ ├── etcd.go │ ├── gateway.go │ ├── grpc_proxy.go │ ├── help.go │ ├── main.go │ └── util.go │ ├── etcdserver │ ├── adapters.go │ ├── api │ │ ├── capability.go │ │ ├── cluster.go │ │ ├── doc.go │ │ ├── etcdhttp │ │ │ ├── debug.go │ │ │ ├── doc.go │ │ │ ├── health.go │ │ │ ├── health_test.go │ │ │ ├── metrics.go │ │ │ ├── peer.go │ │ │ ├── peer_test.go │ │ │ ├── types │ │ │ │ ├── errors.go │ │ │ │ └── errors_test.go │ │ │ ├── utils.go │ │ │ ├── version.go │ │ │ └── version_test.go │ │ ├── membership │ │ │ ├── cluster.go │ │ │ ├── cluster_opts.go │ │ │ ├── cluster_test.go │ │ │ ├── doc.go │ │ │ ├── errors.go │ │ │ ├── member.go │ │ │ ├── member_test.go │ │ │ ├── membership_test.go │ │ │ ├── metrics.go │ │ │ ├── store.go │ │ │ ├── storev2.go │ │ │ └── storev2_test.go │ │ ├── rafthttp │ │ │ ├── coder.go │ │ │ ├── doc.go │ │ │ ├── fake_roundtripper_test.go │ │ │ ├── functional_test.go │ │ │ ├── http.go │ │ │ ├── http_test.go │ │ │ ├── metrics.go │ │ │ ├── msg_codec.go │ │ │ ├── msg_codec_test.go │ │ │ ├── msgappv2_codec.go │ │ │ ├── msgappv2_codec_test.go │ │ │ ├── peer.go │ │ │ ├── peer_status.go │ │ │ ├── peer_test.go │ │ │ ├── pipeline.go │ │ │ ├── pipeline_test.go │ │ │ ├── probing_status.go │ │ │ ├── remote.go │ │ │ ├── snapshot_sender.go │ │ │ ├── snapshot_test.go │ │ │ ├── stream.go │ │ │ ├── stream_test.go │ │ │ ├── transport.go │ │ │ ├── transport_bench_test.go │ │ │ ├── transport_test.go │ │ │ ├── urlpick.go │ │ │ ├── urlpick_test.go │ │ │ ├── util.go │ │ │ └── util_test.go │ │ ├── snap │ │ │ ├── db.go │ │ │ ├── doc.go │ │ │ ├── message.go │ │ │ ├── metrics.go │ │ │ ├── snappb │ │ │ │ ├── snap.pb.go │ │ │ │ └── snap.proto │ │ │ ├── snapshotter.go │ │ │ └── snapshotter_test.go │ │ ├── v2discovery │ │ │ ├── discovery.go │ │ │ └── discovery_test.go │ │ ├── v2error │ │ │ ├── error.go │ │ │ └── error_test.go │ │ ├── v2stats │ │ │ ├── leader.go │ │ │ ├── queue.go │ │ │ └── server.go │ │ ├── v2store │ │ │ ├── doc.go │ │ │ ├── event.go │ │ │ ├── event_history.go │ │ │ ├── event_queue.go │ │ │ ├── event_test.go │ │ │ ├── heap_test.go │ │ │ ├── metrics.go │ │ │ ├── node.go │ │ │ ├── node_extern.go │ │ │ ├── node_extern_test.go │ │ │ ├── node_test.go │ │ │ ├── stats.go │ │ │ ├── stats_test.go │ │ │ ├── store.go │ │ │ ├── store_bench_test.go │ │ │ ├── store_ttl_test.go │ │ │ ├── ttl_key_heap.go │ │ │ ├── watcher.go │ │ │ ├── watcher_hub.go │ │ │ ├── watcher_hub_test.go │ │ │ └── watcher_test.go │ │ ├── v3alarm │ │ │ └── alarms.go │ │ ├── v3client │ │ │ ├── doc.go │ │ │ └── v3client.go │ │ ├── v3compactor │ │ │ ├── compactor.go │ │ │ ├── compactor_test.go │ │ │ ├── doc.go │ │ │ ├── periodic.go │ │ │ ├── periodic_test.go │ │ │ ├── revision.go │ │ │ └── revision_test.go │ │ ├── v3discovery │ │ │ ├── discovery.go │ │ │ └── discovery_test.go │ │ ├── v3election │ │ │ ├── doc.go │ │ │ ├── election.go │ │ │ └── v3electionpb │ │ │ │ ├── gw │ │ │ │ └── v3election.pb.gw.go │ │ │ │ ├── v3election.pb.go │ │ │ │ └── v3election.proto │ │ ├── v3lock │ │ │ ├── doc.go │ │ │ ├── lock.go │ │ │ └── v3lockpb │ │ │ │ ├── gw │ │ │ │ └── v3lock.pb.gw.go │ │ │ │ ├── v3lock.pb.go │ │ │ │ └── v3lock.proto │ │ └── v3rpc │ │ │ ├── auth.go │ │ │ ├── codec.go │ │ │ ├── grpc.go │ │ │ ├── header.go │ │ │ ├── interceptor.go │ │ │ ├── key.go │ │ │ ├── key_test.go │ │ │ ├── lease.go │ │ │ ├── maintenance.go │ │ │ ├── member.go │ │ │ ├── metrics.go │ │ │ ├── quota.go │ │ │ ├── util.go │ │ │ ├── util_test.go │ │ │ ├── validationfuzz_test.go │ │ │ ├── watch.go │ │ │ └── watch_test.go │ ├── apply │ │ ├── apply.go │ │ ├── apply_auth.go │ │ ├── corrupt.go │ │ ├── metrics.go │ │ └── uber_applier.go │ ├── apply_v2.go │ ├── bootstrap.go │ ├── bootstrap_test.go │ ├── cindex │ │ ├── cindex.go │ │ ├── cindex_test.go │ │ └── doc.go │ ├── cluster_util.go │ ├── cluster_util_test.go │ ├── corrupt.go │ ├── corrupt_test.go │ ├── doc.go │ ├── errors │ │ └── errors.go │ ├── metrics.go │ ├── raft.go │ ├── raft_test.go │ ├── server.go │ ├── server_access_control.go │ ├── server_test.go │ ├── snapshot_merge.go │ ├── txn │ │ ├── metrics.go │ │ ├── txn.go │ │ ├── txn_test.go │ │ ├── util.go │ │ └── util_bench_test.go │ ├── util.go │ ├── util_test.go │ ├── v2_server.go │ ├── v3_server.go │ ├── version │ │ ├── doc.go │ │ ├── downgrade.go │ │ ├── downgrade_test.go │ │ ├── errors.go │ │ ├── monitor.go │ │ ├── monitor_test.go │ │ ├── version.go │ │ └── version_test.go │ ├── zap_raft.go │ └── zap_raft_test.go │ ├── go.mod │ ├── go.sum │ ├── lease │ ├── doc.go │ ├── lease.go │ ├── lease_queue.go │ ├── lease_queue_test.go │ ├── leasehttp │ │ ├── doc.go │ │ ├── http.go │ │ └── http_test.go │ ├── leasepb │ │ ├── lease.pb.go │ │ └── lease.proto │ ├── lessor.go │ ├── lessor_bench_test.go │ ├── lessor_test.go │ └── metrics.go │ ├── main.go │ ├── main_test.go │ ├── proxy │ ├── grpcproxy │ │ ├── adapter │ │ │ ├── auth_client_adapter.go │ │ │ ├── chan_stream.go │ │ │ ├── cluster_client_adapter.go │ │ │ ├── doc.go │ │ │ ├── election_client_adapter.go │ │ │ ├── kv_client_adapter.go │ │ │ ├── lease_client_adapter.go │ │ │ ├── lock_client_adapter.go │ │ │ ├── maintenance_client_adapter.go │ │ │ └── watch_client_adapter.go │ │ ├── auth.go │ │ ├── cache │ │ │ └── store.go │ │ ├── cluster.go │ │ ├── doc.go │ │ ├── election.go │ │ ├── health.go │ │ ├── kv.go │ │ ├── leader.go │ │ ├── lease.go │ │ ├── lock.go │ │ ├── maintenance.go │ │ ├── metrics.go │ │ ├── register.go │ │ ├── util.go │ │ ├── watch.go │ │ ├── watch_broadcast.go │ │ ├── watch_broadcasts.go │ │ ├── watch_ranges.go │ │ └── watcher.go │ └── tcpproxy │ │ ├── doc.go │ │ ├── userspace.go │ │ └── userspace_test.go │ ├── storage │ ├── backend.go │ ├── backend │ │ ├── backend.go │ │ ├── backend_bench_test.go │ │ ├── backend_test.go │ │ ├── batch_tx.go │ │ ├── batch_tx_test.go │ │ ├── config_default.go │ │ ├── config_linux.go │ │ ├── config_windows.go │ │ ├── doc.go │ │ ├── export_test.go │ │ ├── hooks.go │ │ ├── hooks_test.go │ │ ├── metrics.go │ │ ├── read_tx.go │ │ ├── testing │ │ │ └── betesting.go │ │ ├── tx_buffer.go │ │ ├── verify.go │ │ └── verify_test.go │ ├── datadir │ │ ├── datadir.go │ │ ├── datadir_test.go │ │ └── doc.go │ ├── hooks.go │ ├── metrics.go │ ├── mvcc │ │ ├── doc.go │ │ ├── hash.go │ │ ├── hash_test.go │ │ ├── index.go │ │ ├── index_bench_test.go │ │ ├── index_test.go │ │ ├── key_index.go │ │ ├── key_index_test.go │ │ ├── kv.go │ │ ├── kv_test.go │ │ ├── kv_view.go │ │ ├── kvstore.go │ │ ├── kvstore_bench_test.go │ │ ├── kvstore_compaction.go │ │ ├── kvstore_compaction_test.go │ │ ├── kvstore_test.go │ │ ├── kvstore_txn.go │ │ ├── metrics.go │ │ ├── metrics_txn.go │ │ ├── revision.go │ │ ├── revision_test.go │ │ ├── store.go │ │ ├── store_test.go │ │ ├── testutil │ │ │ └── hash.go │ │ ├── watchable_store.go │ │ ├── watchable_store_bench_test.go │ │ ├── watchable_store_test.go │ │ ├── watchable_store_txn.go │ │ ├── watcher.go │ │ ├── watcher_bench_test.go │ │ ├── watcher_group.go │ │ └── watcher_test.go │ ├── quota.go │ ├── schema │ │ ├── actions.go │ │ ├── actions_test.go │ │ ├── alarm.go │ │ ├── auth.go │ │ ├── auth_roles.go │ │ ├── auth_roles_test.go │ │ ├── auth_test.go │ │ ├── auth_users.go │ │ ├── auth_users_test.go │ │ ├── bucket.go │ │ ├── changes.go │ │ ├── changes_test.go │ │ ├── cindex.go │ │ ├── confstate.go │ │ ├── confstate_test.go │ │ ├── lease.go │ │ ├── lease_test.go │ │ ├── membership.go │ │ ├── migration.go │ │ ├── migration_test.go │ │ ├── schema.go │ │ ├── schema_test.go │ │ ├── version.go │ │ └── version_test.go │ ├── storage.go │ ├── util.go │ └── wal │ │ ├── decoder.go │ │ ├── doc.go │ │ ├── encoder.go │ │ ├── file_pipeline.go │ │ ├── file_pipeline_test.go │ │ ├── metrics.go │ │ ├── record_test.go │ │ ├── repair.go │ │ ├── repair_test.go │ │ ├── testing │ │ └── waltesting.go │ │ ├── util.go │ │ ├── version.go │ │ ├── version_test.go │ │ ├── wal.go │ │ ├── wal_bench_test.go │ │ ├── wal_test.go │ │ └── walpb │ │ ├── record.go │ │ ├── record.pb.go │ │ ├── record.proto │ │ └── record_test.go │ └── verify │ ├── doc.go │ └── verify.go ├── go.mod ├── go.sum ├── logger └── logger.go ├── main.go ├── memdb ├── btree.go ├── command.go ├── concurrentmap.go ├── db.go ├── dblock.go ├── hash.go ├── hash_struct.go ├── keys.go ├── keys_test.go ├── list.go ├── list_struct.go ├── list_test.go ├── pubsub.go ├── pubsub_struct.go ├── raft_command.go ├── sets.go ├── sets_struct.go ├── sorted_set.go ├── sorted_set_struct.go ├── sorted_set_test.go ├── stream.go ├── stream_struct.go ├── stream_test.go ├── string.go └── string_test.go ├── raftexample ├── README.md ├── doc.go ├── httpapi.go ├── kvstore.go ├── kvstore_test.go ├── listener.go ├── raft.go └── raft_test.go ├── redis.conf ├── redis2.conf ├── resp ├── errors_singleton.go ├── parser.go ├── parser_test.go └── structure.go ├── server ├── cmd_middleware.go ├── cmd_middleware_test.go ├── db_manager.go └── server.go └── util ├── chan_pump.go ├── util.go └── util_test.go /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/.idea 2 | **/test 3 | **/.vscode 4 | **/.VSCodeCounter 5 | *.log 6 | *.rdb 7 | *.tmp 8 | *.wal 9 | 10 | # build binaries 11 | RedisGO 12 | oryxBuildBinary 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Jeff Zhong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /RedisGO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/RedisGO -------------------------------------------------------------------------------- /benchmark.sh: -------------------------------------------------------------------------------- 1 | 2 | i="GET,SET,INCR,DECR,LPUSH,RPUSH,LPOP,RPOP,SADD,HSET,SPOP,MSET" 3 | redis-benchmark -c 50 -n 200000 -t "$i" -q -p 6380 4 | redis-benchmark -c 50 -n 200000 -t "$i" -q -p 6379 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /benchmark2.sh: -------------------------------------------------------------------------------- 1 | clicmd1="redis-cli -p 6379" 2 | clicmd2="redis-cli -p 6380" 3 | 4 | echo flushall | eval "$clicmd1" 5 | echo flushall | eval "$clicmd2" 6 | echo zadd a 1 a 2 b 3 c 40 d 50 f |eval "$clicmd1" 7 | echo zadd a 1 a 2 b 3 c 40 d 50 f |eval "$clicmd2" 8 | 9 | redis-benchmark -q -p 6379 zrange a 0 100 withscores 10 | redis-benchmark -q -p 6380 zrange a 0 100 withscores 11 | 12 | -------------------------------------------------------------------------------- /cluster_config.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsCluster": true, 3 | "PeerAddrs": "http://127.0.0.1:16380", 4 | "RaftAddr": "", 5 | "PeerIDs": "1", 6 | "NodeID": 1, 7 | "KVPort": 6380, 8 | "JoinCluster": false 9 | } -------------------------------------------------------------------------------- /cluster_config2.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsCluster": true, 3 | "PeerAddrs": "http://127.0.0.1:16380,http://127.0.0.1:16390", 4 | "RaftAddr": "", 5 | "PeerIDs": "1,2", 6 | "NodeID": 2, 7 | "KVPort": 6383, 8 | "JoinCluster": false 9 | } -------------------------------------------------------------------------------- /cluster_test/RedisGO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/cluster_test/RedisGO -------------------------------------------------------------------------------- /cluster_test/cluster_config.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsCluster": true, 3 | "PeerAddrs": "http://127.0.0.1:16380,http://127.0.0.1:16381,http://127.0.0.1:16382", 4 | "RaftAddr": "", 5 | "PeerIDs": "1,2,3", 6 | "NodeID": 1, 7 | "KVPort": 6380, 8 | "JoinCluster": false 9 | } -------------------------------------------------------------------------------- /cluster_test/cluster_config1.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsCluster": true, 3 | "PeerAddrs": "http://127.0.0.1:16380,http://127.0.0.1:16381,http://127.0.0.1:16382", 4 | "RaftAddr": "", 5 | "PeerIDs": "1,2,3", 6 | "NodeID": 2, 7 | "KVPort": 6381, 8 | "JoinCluster": false 9 | } -------------------------------------------------------------------------------- /cluster_test/cluster_config2.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsCluster": true, 3 | "PeerAddrs": "http://127.0.0.1:16380,http://127.0.0.1:16381,http://127.0.0.1:16382", 4 | "RaftAddr": "", 5 | "PeerIDs": "1,2,3", 6 | "NodeID": 3, 7 | "KVPort": 6382, 8 | "JoinCluster": false 9 | } -------------------------------------------------------------------------------- /cluster_test/cluster_config3.json: -------------------------------------------------------------------------------- 1 | { 2 | "IsCluster": true, 3 | "PeerAddrs": "http://127.0.0.1:16380,http://127.0.0.1:16381,http://127.0.0.1:16382,http://127.0.0.1:16390", 4 | "RaftAddr": "http://127.0.0.1:16390", 5 | "PeerIDs": "1,2,3,4", 6 | "NodeID": 4, 7 | "KVPort": 6383, 8 | "JoinCluster": true 9 | } -------------------------------------------------------------------------------- /cluster_test/cluster_test.sh: -------------------------------------------------------------------------------- 1 | go build .. 2 | ./RedisGO --IsCluster=true --ClusterConfigPath="./cluster_config.json" --config="./redis.conf" & 3 | ./RedisGO --IsCluster=true --ClusterConfigPath="./cluster_config1.json" --config="./redis1.conf" & 4 | ./RedisGO --IsCluster=true --ClusterConfigPath="./cluster_config2.json" --config="./redis2.conf" 5 | 6 | #./RedisGO --IsCluster=true --ClusterConfigPath="./cluster_config3.json" --config="./redis3.conf" -------------------------------------------------------------------------------- /cluster_test/cluster_test_new_node_join.sh: -------------------------------------------------------------------------------- 1 | ./RedisGO --IsCluster=true --ClusterConfigPath="./cluster_config3.json" --config="./redis3.conf" -------------------------------------------------------------------------------- /cluster_test/redis.conf: -------------------------------------------------------------------------------- 1 | host 127.0.0.1 2 | 3 | port 6380 4 | 5 | logdir /tmp 6 | 7 | loglevel info 8 | 9 | shardnum 1024 10 | 11 | appendonly yes 12 | 13 | databases 16 -------------------------------------------------------------------------------- /cluster_test/redis1.conf: -------------------------------------------------------------------------------- 1 | host 127.0.0.1 2 | 3 | port 6381 4 | 5 | logdir /tmp 6 | 7 | 8 | loglevel info 9 | 10 | shardnum 1024 11 | 12 | appendonly yes 13 | 14 | databases 16 -------------------------------------------------------------------------------- /cluster_test/redis2.conf: -------------------------------------------------------------------------------- 1 | host 127.0.0.1 2 | 3 | port 6382 4 | 5 | logdir /tmp 6 | 7 | loglevel info 8 | 9 | shardnum 1024 10 | 11 | appendonly yes 12 | 13 | databases 16 -------------------------------------------------------------------------------- /cluster_test/redis3.conf: -------------------------------------------------------------------------------- 1 | host 127.0.0.1 2 | 3 | port 6383 4 | 5 | logdir /tmp 6 | 7 | loglevel info 8 | 9 | shardnum 1024 10 | 11 | appendonly yes 12 | 13 | databases 16 -------------------------------------------------------------------------------- /config/config_test.go: -------------------------------------------------------------------------------- 1 | package config 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestConfig_Parse(t *testing.T) { 9 | cfg := new(Config) 10 | err := cfg.Parse("./RedisGO.conf") 11 | if err != nil { 12 | t.Error(err) 13 | } 14 | if cfg.Host != "127.0.0.1" { 15 | t.Error(fmt.Sprintf("cfg.Host == %s, expect 127.0.0.1", cfg.Host)) 16 | } 17 | if cfg.Port != 6399 { 18 | t.Error(fmt.Sprintf("cfg.Port == %d, expect 6399", cfg.Port)) 19 | } 20 | if cfg.LogDir != "/tmp" { 21 | t.Error(fmt.Sprintf("cfg.LogDir == %s, expect /tmp", cfg.LogDir)) 22 | } 23 | if cfg.LogLevel != "info" { 24 | t.Error(fmt.Sprintf("cfg.LogLevel == %s, expect info", cfg.LogLevel)) 25 | } 26 | if cfg.ShardNum != 1024 { 27 | t.Error(fmt.Sprintf("cfg.ShardNum == %d, expect 1024", cfg.ShardNum)) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /etcd/.gitignore: -------------------------------------------------------------------------------- 1 | /agent-* 2 | /coverage 3 | /covdir 4 | /gopath 5 | /gopath.proto 6 | /release 7 | /bin 8 | *.etcd 9 | *.log 10 | *.swp 11 | /etcd 12 | /hack/insta-discovery/.env 13 | *.coverprofile 14 | *.test 15 | hack/tls-setup/certs 16 | .idea 17 | *.iml 18 | /contrib/mixin/manifests 19 | /contrib/raftexample/raftexample 20 | /contrib/raftexample/raftexample-* 21 | /vendor 22 | /tests/e2e/default.proxy 23 | *.tmp 24 | *.bak 25 | .gobincache/ 26 | .DS_Store 27 | /Documentation/dev-guide/api_reference_v3.md 28 | /Documentation/dev-guide/api_concurrency_reference_v3.md 29 | 30 | /tools/etcd-dump-db/etcd-dump-db 31 | /tools/etcd-dump-logs/etcd-dump-logs 32 | /tools/etcd-dump-metrics/etcd-dump-metrics 33 | /tools/local-tester/bridge/bridge 34 | /tools/proto-annotations/proto-annotations 35 | /tools/benchmark/benchmark 36 | /out 37 | -------------------------------------------------------------------------------- /etcd/api/authpb/auth.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package authpb; 3 | 4 | import "gogoproto/gogo.proto"; 5 | 6 | option (gogoproto.marshaler_all) = true; 7 | option (gogoproto.sizer_all) = true; 8 | option (gogoproto.unmarshaler_all) = true; 9 | option (gogoproto.goproto_getters_all) = false; 10 | option (gogoproto.goproto_enum_prefix_all) = false; 11 | 12 | message UserAddOptions { 13 | bool no_password = 1; 14 | }; 15 | 16 | // User is a single entry in the bucket authUsers 17 | message User { 18 | bytes name = 1; 19 | bytes password = 2; 20 | repeated string roles = 3; 21 | UserAddOptions options = 4; 22 | } 23 | 24 | // Permission is a single entity 25 | message Permission { 26 | enum Type { 27 | READ = 0; 28 | WRITE = 1; 29 | READWRITE = 2; 30 | } 31 | Type permType = 1; 32 | 33 | bytes key = 2; 34 | bytes range_end = 3; 35 | } 36 | 37 | // Role is a single entry in the bucket authRoles 38 | message Role { 39 | bytes name = 1; 40 | 41 | repeated Permission keyPermission = 2; 42 | } 43 | -------------------------------------------------------------------------------- /etcd/api/etcdserverpb/raft_internal_stringer_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 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 | 15 | package etcdserverpb_test 16 | 17 | import ( 18 | "testing" 19 | 20 | pb "go.etcd.io/etcd/api/v3/etcdserverpb" 21 | ) 22 | 23 | // TestInvalidGoYypeIntPanic tests conditions that caused 24 | // panic: invalid Go type int for field k8s_io.kubernetes.vendor.go_etcd_io.etcd.etcdserver.etcdserverpb.loggablePutRequest.value_size 25 | // See https://github.com/kubernetes/kubernetes/issues/91937 for more details 26 | func TestInvalidGoTypeIntPanic(t *testing.T) { 27 | result := pb.NewLoggablePutRequest(&pb.PutRequest{}).String() 28 | if result != "" { 29 | t.Errorf("Got result: %s, expected empty string", result) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /etcd/api/go.mod: -------------------------------------------------------------------------------- 1 | module go.etcd.io/etcd/api/v3 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/coreos/go-semver v0.3.0 7 | github.com/gogo/protobuf v1.3.2 8 | github.com/golang/protobuf v1.5.2 9 | github.com/grpc-ecosystem/grpc-gateway v1.16.0 10 | github.com/stretchr/testify v1.7.2 11 | google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 12 | google.golang.org/grpc v1.47.0 13 | ) 14 | 15 | require ( 16 | github.com/davecgh/go-spew v1.1.0 // indirect 17 | github.com/pmezard/go-difflib v1.0.0 // indirect 18 | golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect 19 | golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect 20 | golang.org/x/text v0.3.5 // indirect 21 | google.golang.org/protobuf v1.27.1 // indirect 22 | gopkg.in/yaml.v2 v2.4.0 // indirect 23 | gopkg.in/yaml.v3 v3.0.1 // indirect 24 | ) 25 | 26 | // Bad imports are sometimes causing attempts to pull that code. 27 | // This makes the error more explicit. 28 | replace ( 29 | go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY 30 | go.etcd.io/etcd/api/v3 => ./FORBIDDEN_DEPENDENCY 31 | go.etcd.io/etcd/pkg/v3 => ./FORBIDDEN_DEPENDENCY 32 | go.etcd.io/etcd/tests/v3 => ./FORBIDDEN_DEPENDENCY 33 | go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY 34 | ) 35 | -------------------------------------------------------------------------------- /etcd/api/v3rpc/rpctypes/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package rpctypes has types and values shared by the etcd server and client for v3 RPC interaction. 16 | package rpctypes 17 | -------------------------------------------------------------------------------- /etcd/api/v3rpc/rpctypes/md.go: -------------------------------------------------------------------------------- 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 | 15 | package rpctypes 16 | 17 | var ( 18 | MetadataRequireLeaderKey = "hasleader" 19 | MetadataHasLeader = "true" 20 | 21 | MetadataClientAPIVersionKey = "client-api-version" 22 | ) 23 | -------------------------------------------------------------------------------- /etcd/api/v3rpc/rpctypes/metadatafields.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package rpctypes 16 | 17 | var ( 18 | TokenFieldNameGRPC = "token" 19 | TokenFieldNameSwagger = "authorization" 20 | ) 21 | -------------------------------------------------------------------------------- /etcd/api/versionpb/version.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package versionpb; 3 | 4 | import "gogoproto/gogo.proto"; 5 | import "google/protobuf/descriptor.proto"; 6 | 7 | option (gogoproto.marshaler_all) = true; 8 | option (gogoproto.unmarshaler_all) = true; 9 | 10 | // Indicates etcd version that introduced the message, used to determine minimal etcd version required to interpret wal that includes this message. 11 | extend google.protobuf.MessageOptions { 12 | optional string etcd_version_msg = 50000; 13 | } 14 | 15 | // Indicates etcd version that introduced the field, used to determine minimal etcd version required to interpret wal that sets this field. 16 | extend google.protobuf.FieldOptions { 17 | optional string etcd_version_field = 50001; 18 | } 19 | 20 | // Indicates etcd version that introduced the enum, used to determine minimal etcd version required to interpret wal that uses this enum. 21 | extend google.protobuf.EnumOptions { 22 | optional string etcd_version_enum = 50002; 23 | } 24 | 25 | // Indicates etcd version that introduced the enum value, used to determine minimal etcd version required to interpret wal that sets this enum value. 26 | extend google.protobuf.EnumValueOptions { 27 | optional string etcd_version_enum_value = 50003; 28 | } 29 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/dir_unix.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !windows 16 | // +build !windows 17 | 18 | package fileutil 19 | 20 | import "os" 21 | 22 | const ( 23 | // PrivateDirMode grants owner to make/remove files inside the directory. 24 | PrivateDirMode = 0700 25 | ) 26 | 27 | // OpenDir opens a directory for syncing. 28 | func OpenDir(path string) (*os.File, error) { return os.Open(path) } 29 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package fileutil implements utility functions related to files and paths. 16 | package fileutil 17 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/filereader_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 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 | 15 | package fileutil 16 | 17 | import ( 18 | "os" 19 | "strings" 20 | "testing" 21 | 22 | "github.com/stretchr/testify/assert" 23 | ) 24 | 25 | func TestFileBufReader(t *testing.T) { 26 | f, err := os.CreateTemp(t.TempDir(), "wal") 27 | if err != nil { 28 | t.Errorf("Unexpected error: %v", err) 29 | } 30 | fi, err := f.Stat() 31 | if err != nil { 32 | t.Errorf("Unexpected error: %v", err) 33 | } 34 | 35 | fbr := NewFileBufReader(NewFileReader(f)) 36 | 37 | if !strings.HasPrefix(fbr.FileInfo().Name(), "wal") { 38 | t.Errorf("Unexpected file name: %s", fbr.FileInfo().Name()) 39 | } 40 | assert.Equal(t, fi.Size(), fbr.FileInfo().Size()) 41 | assert.Equal(t, fi.IsDir(), fbr.FileInfo().IsDir()) 42 | assert.Equal(t, fi.Mode(), fbr.FileInfo().Mode()) 43 | assert.Equal(t, fi.ModTime(), fbr.FileInfo().ModTime()) 44 | } 45 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/lock.go: -------------------------------------------------------------------------------- 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 | 15 | package fileutil 16 | 17 | import ( 18 | "errors" 19 | "os" 20 | ) 21 | 22 | var ( 23 | ErrLocked = errors.New("fileutil: file already locked") 24 | ) 25 | 26 | type LockedFile struct{ *os.File } 27 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/lock_linux_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | //go:build linux 16 | // +build linux 17 | 18 | package fileutil 19 | 20 | import "testing" 21 | 22 | // TestLockAndUnlockSyscallFlock tests the fallback flock using the flock syscall. 23 | func TestLockAndUnlockSyscallFlock(t *testing.T) { 24 | oldTryLock, oldLock := linuxTryLockFile, linuxLockFile 25 | defer func() { 26 | linuxTryLockFile, linuxLockFile = oldTryLock, oldLock 27 | }() 28 | linuxTryLockFile, linuxLockFile = flockTryLockFile, flockLockFile 29 | TestLockAndUnlock(t) 30 | } 31 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/lock_plan9.go: -------------------------------------------------------------------------------- 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 | 15 | package fileutil 16 | 17 | import ( 18 | "os" 19 | "syscall" 20 | "time" 21 | ) 22 | 23 | func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { 24 | if err := os.Chmod(path, syscall.DMEXCL|PrivateFileMode); err != nil { 25 | return nil, err 26 | } 27 | f, err := os.Open(path, flag, perm) 28 | if err != nil { 29 | return nil, ErrLocked 30 | } 31 | return &LockedFile{f}, nil 32 | } 33 | 34 | func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { 35 | if err := os.Chmod(path, syscall.DMEXCL|PrivateFileMode); err != nil { 36 | return nil, err 37 | } 38 | for { 39 | f, err := os.OpenFile(path, flag, perm) 40 | if err == nil { 41 | return &LockedFile{f}, nil 42 | } 43 | time.Sleep(10 * time.Millisecond) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/lock_unix.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !windows && !plan9 && !solaris && !linux 16 | // +build !windows,!plan9,!solaris,!linux 17 | 18 | package fileutil 19 | 20 | import ( 21 | "os" 22 | ) 23 | 24 | func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { 25 | return flockTryLockFile(path, flag, perm) 26 | } 27 | 28 | func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { 29 | return flockLockFile(path, flag, perm) 30 | } 31 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/preallocate_unsupported.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !linux && !darwin 16 | // +build !linux,!darwin 17 | 18 | package fileutil 19 | 20 | import "os" 21 | 22 | func preallocExtend(f *os.File, sizeInBytes int64) error { 23 | return preallocExtendTrunc(f, sizeInBytes) 24 | } 25 | 26 | func preallocFixed(f *os.File, sizeInBytes int64) error { return nil } 27 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/sync.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !linux && !darwin 16 | // +build !linux,!darwin 17 | 18 | package fileutil 19 | 20 | import "os" 21 | 22 | // Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform. 23 | func Fsync(f *os.File) error { 24 | return f.Sync() 25 | } 26 | 27 | // Fdatasync is a wrapper around file.Sync(). Special handling is needed on linux platform. 28 | func Fdatasync(f *os.File) error { 29 | return f.Sync() 30 | } 31 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/sync_darwin.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build darwin 16 | // +build darwin 17 | 18 | package fileutil 19 | 20 | import ( 21 | "os" 22 | 23 | "golang.org/x/sys/unix" 24 | ) 25 | 26 | // Fsync on HFS/OSX flushes the data on to the physical drive but the drive 27 | // may not write it to the persistent media for quite sometime and it may be 28 | // written in out-of-order sequence. Using F_FULLFSYNC ensures that the 29 | // physical drive's buffer will also get flushed to the media. 30 | func Fsync(f *os.File) error { 31 | _, err := unix.FcntlInt(f.Fd(), unix.F_FULLFSYNC, 0) 32 | return err 33 | } 34 | 35 | // Fdatasync on darwin platform invokes fcntl(F_FULLFSYNC) for actual persistence 36 | // on physical drive media. 37 | func Fdatasync(f *os.File) error { 38 | return Fsync(f) 39 | } 40 | -------------------------------------------------------------------------------- /etcd/client/pkg/fileutil/sync_linux.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build linux 16 | // +build linux 17 | 18 | package fileutil 19 | 20 | import ( 21 | "os" 22 | "syscall" 23 | ) 24 | 25 | // Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform. 26 | func Fsync(f *os.File) error { 27 | return f.Sync() 28 | } 29 | 30 | // Fdatasync is similar to fsync(), but does not flush modified metadata 31 | // unless that metadata is needed in order to allow a subsequent data retrieval 32 | // to be correctly handled. 33 | func Fdatasync(f *os.File) error { 34 | return syscall.Fdatasync(int(f.Fd())) 35 | } 36 | -------------------------------------------------------------------------------- /etcd/client/pkg/go.mod: -------------------------------------------------------------------------------- 1 | module go.etcd.io/etcd/client/pkg/v3 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/coreos/go-systemd/v22 v22.3.2 7 | github.com/stretchr/testify v1.7.2 8 | go.uber.org/zap v1.21.0 9 | golang.org/x/sys v0.0.0-20210603125802-9665404d3644 10 | ) 11 | 12 | require ( 13 | github.com/benbjohnson/clock v1.1.0 // indirect 14 | github.com/davecgh/go-spew v1.1.1 // indirect 15 | github.com/pmezard/go-difflib v1.0.0 // indirect 16 | go.uber.org/atomic v1.7.0 // indirect 17 | go.uber.org/multierr v1.6.0 // indirect 18 | gopkg.in/yaml.v3 v3.0.1 // indirect 19 | ) 20 | -------------------------------------------------------------------------------- /etcd/client/pkg/logutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package logutil includes utilities to facilitate logging. 16 | package logutil 17 | -------------------------------------------------------------------------------- /etcd/client/pkg/logutil/log_format.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package logutil 16 | 17 | import "fmt" 18 | 19 | const ( 20 | JsonLogFormat = "json" 21 | ConsoleLogFormat = "console" 22 | ) 23 | 24 | var DefaultLogFormat = JsonLogFormat 25 | 26 | // ConvertToZapFormat converts and validated log format string. 27 | func ConvertToZapFormat(format string) (string, error) { 28 | switch format { 29 | case ConsoleLogFormat: 30 | return ConsoleLogFormat, nil 31 | case JsonLogFormat: 32 | return JsonLogFormat, nil 33 | case "": 34 | return DefaultLogFormat, nil 35 | default: 36 | return "", fmt.Errorf("unknown log format: %s, supported values json, console", format) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etcd/client/pkg/logutil/log_format_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package logutil 16 | 17 | import ( 18 | "testing" 19 | ) 20 | 21 | func TestLogFormat(t *testing.T) { 22 | tests := []struct { 23 | given string 24 | want string 25 | errExpected bool 26 | }{ 27 | {"json", JsonLogFormat, false}, 28 | {"console", ConsoleLogFormat, false}, 29 | {"", JsonLogFormat, false}, 30 | {"konsole", "", true}, 31 | } 32 | 33 | for i, tt := range tests { 34 | got, err := ConvertToZapFormat(tt.given) 35 | if got != tt.want { 36 | t.Errorf("#%d: ConvertToZapFormat failure: want=%v, got=%v", i, tt.want, got) 37 | } 38 | 39 | if err != nil { 40 | if !tt.errExpected { 41 | t.Errorf("#%d: ConvertToZapFormat unexpected error: %v", i, err) 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /etcd/client/pkg/logutil/log_level.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package logutil 16 | 17 | import ( 18 | "go.uber.org/zap/zapcore" 19 | ) 20 | 21 | var DefaultLogLevel = "info" 22 | 23 | // ConvertToZapLevel converts log level string to zapcore.Level. 24 | func ConvertToZapLevel(lvl string) zapcore.Level { 25 | var level zapcore.Level 26 | if err := level.Set(lvl); err != nil { 27 | panic(err) 28 | } 29 | return level 30 | } 31 | -------------------------------------------------------------------------------- /etcd/client/pkg/logutil/zap_journal_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | //go:build !windows 16 | // +build !windows 17 | 18 | package logutil 19 | 20 | import ( 21 | "bytes" 22 | "testing" 23 | 24 | "go.uber.org/zap" 25 | "go.uber.org/zap/zapcore" 26 | ) 27 | 28 | func TestNewJournalWriter(t *testing.T) { 29 | buf := bytes.NewBuffer(nil) 30 | jw, err := NewJournalWriter(buf) 31 | if err != nil { 32 | t.Skip(err) 33 | } 34 | 35 | syncer := zapcore.AddSync(jw) 36 | 37 | cr := zapcore.NewCore( 38 | zapcore.NewJSONEncoder(DefaultZapLoggerConfig.EncoderConfig), 39 | syncer, 40 | zap.NewAtomicLevelAt(zap.InfoLevel), 41 | ) 42 | 43 | lg := zap.New(cr, zap.AddCaller(), zap.ErrorOutput(syncer)) 44 | defer lg.Sync() 45 | 46 | lg.Info("TestNewJournalWriter") 47 | if buf.String() == "" { 48 | // check with "journalctl -f" 49 | t.Log("sent logs successfully to journald") 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /etcd/client/pkg/pathutil/path.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package pathutil implements utility functions for handling slash-separated 6 | // paths. 7 | package pathutil 8 | 9 | import "path" 10 | 11 | // CanonicalURLPath returns the canonical url path for p, which follows the rules: 12 | // 1. the path always starts with "/" 13 | // 2. replace multiple slashes with a single slash 14 | // 3. replace each '.' '..' path name element with equivalent one 15 | // 4. keep the trailing slash 16 | // The function is borrowed from stdlib http.cleanPath in server.go. 17 | func CanonicalURLPath(p string) string { 18 | if p == "" { 19 | return "/" 20 | } 21 | if p[0] != '/' { 22 | p = "/" + p 23 | } 24 | np := path.Clean(p) 25 | // path.Clean removes trailing slash except for root, 26 | // put the trailing slash back if necessary. 27 | if p[len(p)-1] == '/' && np != "/" { 28 | np += "/" 29 | } 30 | return np 31 | } 32 | -------------------------------------------------------------------------------- /etcd/client/pkg/pathutil/path_test.go: -------------------------------------------------------------------------------- 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 | 15 | package pathutil 16 | 17 | import "testing" 18 | 19 | func TestCanonicalURLPath(t *testing.T) { 20 | tests := []struct { 21 | p string 22 | wp string 23 | }{ 24 | {"/a", "/a"}, 25 | {"", "/"}, 26 | {"a", "/a"}, 27 | {"//a", "/a"}, 28 | {"/a/.", "/a"}, 29 | {"/a/..", "/"}, 30 | {"/a/", "/a/"}, 31 | {"/a//", "/a/"}, 32 | } 33 | for i, tt := range tests { 34 | if g := CanonicalURLPath(tt.p); g != tt.wp { 35 | t.Errorf("#%d: canonical path = %s, want %s", i, g, tt.wp) 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /etcd/client/pkg/systemd/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package systemd provides utility functions for systemd. 16 | package systemd 17 | -------------------------------------------------------------------------------- /etcd/client/pkg/systemd/journal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package systemd 16 | 17 | import "net" 18 | 19 | // DialJournal returns no error if the process can dial journal socket. 20 | // Returns an error if dial failed, which indicates journald is not available 21 | // (e.g. run embedded etcd as docker daemon). 22 | // Reference: https://github.com/coreos/go-systemd/blob/master/journal/journal.go. 23 | func DialJournal() error { 24 | conn, err := net.Dial("unixgram", "/run/systemd/journal/socket") 25 | if conn != nil { 26 | defer conn.Close() 27 | } 28 | return err 29 | } 30 | -------------------------------------------------------------------------------- /etcd/client/pkg/testutil/leak_test.go: -------------------------------------------------------------------------------- 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 | 15 | package testutil 16 | 17 | import ( 18 | "fmt" 19 | "os" 20 | "testing" 21 | ) 22 | 23 | // so tests pass if given a -run that doesn't include TestSample 24 | var ranSample = false 25 | 26 | func TestMain(m *testing.M) { 27 | m.Run() 28 | isLeaked := CheckLeakedGoroutine() 29 | if ranSample && !isLeaked { 30 | fmt.Fprintln(os.Stderr, "expected leaky goroutines but none is detected") 31 | os.Exit(1) 32 | } 33 | os.Exit(0) 34 | } 35 | 36 | func TestSample(t *testing.T) { 37 | SkipTestIfShortMode(t, "Counting leaked routines is disabled in --short tests") 38 | defer afterTest(t) 39 | ranSample = true 40 | for range make([]struct{}, 100) { 41 | go func() { 42 | select {} 43 | }() 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /etcd/client/pkg/testutil/var.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package testutil 16 | 17 | import "time" 18 | 19 | var ( 20 | ApplyTimeout = time.Second 21 | RequestTimeout = 3 * time.Second 22 | ) 23 | -------------------------------------------------------------------------------- /etcd/client/pkg/tlsutil/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package tlsutil provides utility functions for handling TLS. 16 | package tlsutil 17 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package transport implements various HTTP transport utilities based on Go 16 | // net package. 17 | package transport 18 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/sockopt_solaris.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | //go:build solaris 16 | // +build solaris 17 | 18 | package transport 19 | 20 | import ( 21 | "fmt" 22 | "syscall" 23 | 24 | "golang.org/x/sys/unix" 25 | ) 26 | 27 | func setReusePort(network, address string, c syscall.RawConn) error { 28 | return fmt.Errorf("port reuse is not supported on Solaris") 29 | } 30 | 31 | func setReuseAddress(network, address string, conn syscall.RawConn) error { 32 | return conn.Control(func(fd uintptr) { 33 | syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEADDR, 1) 34 | }) 35 | } 36 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/sockopt_unix.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | //go:build !windows && !solaris 16 | // +build !windows,!solaris 17 | 18 | package transport 19 | 20 | import ( 21 | "syscall" 22 | 23 | "golang.org/x/sys/unix" 24 | ) 25 | 26 | func setReusePort(network, address string, conn syscall.RawConn) error { 27 | return conn.Control(func(fd uintptr) { 28 | syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1) 29 | }) 30 | } 31 | 32 | func setReuseAddress(network, address string, conn syscall.RawConn) error { 33 | return conn.Control(func(fd uintptr) { 34 | syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEADDR, 1) 35 | }) 36 | } 37 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/sockopt_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | //go:build windows 16 | // +build windows 17 | 18 | package transport 19 | 20 | import ( 21 | "fmt" 22 | "syscall" 23 | ) 24 | 25 | func setReusePort(network, address string, c syscall.RawConn) error { 26 | return fmt.Errorf("port reuse is not supported on Windows") 27 | } 28 | 29 | // Windows supports SO_REUSEADDR, but it may cause undefined behavior, as 30 | // there is no protection against port hijacking. 31 | func setReuseAddress(network, addr string, conn syscall.RawConn) error { 32 | return fmt.Errorf("address reuse is not supported on Windows") 33 | } 34 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/timeout_conn.go: -------------------------------------------------------------------------------- 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 | 15 | package transport 16 | 17 | import ( 18 | "net" 19 | "time" 20 | ) 21 | 22 | type timeoutConn struct { 23 | net.Conn 24 | writeTimeout time.Duration 25 | readTimeout time.Duration 26 | } 27 | 28 | func (c timeoutConn) Write(b []byte) (n int, err error) { 29 | if c.writeTimeout > 0 { 30 | if err := c.SetWriteDeadline(time.Now().Add(c.writeTimeout)); err != nil { 31 | return 0, err 32 | } 33 | } 34 | return c.Conn.Write(b) 35 | } 36 | 37 | func (c timeoutConn) Read(b []byte) (n int, err error) { 38 | if c.readTimeout > 0 { 39 | if err := c.SetReadDeadline(time.Now().Add(c.readTimeout)); err != nil { 40 | return 0, err 41 | } 42 | } 43 | return c.Conn.Read(b) 44 | } 45 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/timeout_dialer.go: -------------------------------------------------------------------------------- 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 | 15 | package transport 16 | 17 | import ( 18 | "net" 19 | "time" 20 | ) 21 | 22 | type rwTimeoutDialer struct { 23 | wtimeoutd time.Duration 24 | rdtimeoutd time.Duration 25 | net.Dialer 26 | } 27 | 28 | func (d *rwTimeoutDialer) Dial(network, address string) (net.Conn, error) { 29 | conn, err := d.Dialer.Dial(network, address) 30 | tconn := &timeoutConn{ 31 | readTimeout: d.rdtimeoutd, 32 | writeTimeout: d.wtimeoutd, 33 | Conn: conn, 34 | } 35 | return tconn, err 36 | } 37 | -------------------------------------------------------------------------------- /etcd/client/pkg/transport/unix_listener.go: -------------------------------------------------------------------------------- 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 | 15 | package transport 16 | 17 | import ( 18 | "net" 19 | "os" 20 | ) 21 | 22 | type unixListener struct{ net.Listener } 23 | 24 | func NewUnixListener(addr string) (net.Listener, error) { 25 | if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { 26 | return nil, err 27 | } 28 | l, err := net.Listen("unix", addr) 29 | if err != nil { 30 | return nil, err 31 | } 32 | return &unixListener{l}, nil 33 | } 34 | 35 | func (ul *unixListener) Close() error { 36 | if err := os.Remove(ul.Addr().String()); err != nil && !os.IsNotExist(err) { 37 | return err 38 | } 39 | return ul.Listener.Close() 40 | } 41 | -------------------------------------------------------------------------------- /etcd/client/pkg/types/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package types declares various data types and implements type-checking 16 | // functions. 17 | package types 18 | -------------------------------------------------------------------------------- /etcd/client/pkg/types/id.go: -------------------------------------------------------------------------------- 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 | 15 | package types 16 | 17 | import "strconv" 18 | 19 | // ID represents a generic identifier which is canonically 20 | // stored as a uint64 but is typically represented as a 21 | // base-16 string for input/output 22 | type ID uint64 23 | 24 | func (i ID) String() string { 25 | return strconv.FormatUint(uint64(i), 16) 26 | } 27 | 28 | // IDFromString attempts to create an ID from a base-16 string. 29 | func IDFromString(s string) (ID, error) { 30 | i, err := strconv.ParseUint(s, 16, 64) 31 | return ID(i), err 32 | } 33 | 34 | // IDSlice implements the sort interface 35 | type IDSlice []ID 36 | 37 | func (p IDSlice) Len() int { return len(p) } 38 | func (p IDSlice) Less(i, j int) bool { return uint64(p[i]) < uint64(p[j]) } 39 | func (p IDSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 40 | -------------------------------------------------------------------------------- /etcd/client/pkg/types/slice.go: -------------------------------------------------------------------------------- 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 | 15 | package types 16 | 17 | // Uint64Slice implements sort interface 18 | type Uint64Slice []uint64 19 | 20 | func (p Uint64Slice) Len() int { return len(p) } 21 | func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] } 22 | func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 23 | -------------------------------------------------------------------------------- /etcd/client/pkg/types/slice_test.go: -------------------------------------------------------------------------------- 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 | 15 | package types 16 | 17 | import ( 18 | "reflect" 19 | "sort" 20 | "testing" 21 | ) 22 | 23 | func TestUint64Slice(t *testing.T) { 24 | g := Uint64Slice{10, 500, 5, 1, 100, 25} 25 | w := Uint64Slice{1, 5, 10, 25, 100, 500} 26 | sort.Sort(g) 27 | if !reflect.DeepEqual(g, w) { 28 | t.Errorf("slice after sort = %#v, want %#v", g, w) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /etcd/client/v2/cancelreq.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // borrowed from golang/net/context/ctxhttp/cancelreq.go 6 | 7 | package client 8 | 9 | import "net/http" 10 | 11 | func requestCanceler(tr CancelableTransport, req *http.Request) func() { 12 | ch := make(chan struct{}) 13 | req.Cancel = ch 14 | 15 | return func() { 16 | close(ch) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /etcd/client/v2/cluster_error.go: -------------------------------------------------------------------------------- 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 | 15 | package client 16 | 17 | import "fmt" 18 | 19 | type ClusterError struct { 20 | Errors []error 21 | } 22 | 23 | func (ce *ClusterError) Error() string { 24 | s := ErrClusterUnavailable.Error() 25 | for i, e := range ce.Errors { 26 | s += fmt.Sprintf("; error #%d: %s\n", i, e) 27 | } 28 | return s 29 | } 30 | 31 | func (ce *ClusterError) Detail() string { 32 | s := "" 33 | for i, e := range ce.Errors { 34 | s += fmt.Sprintf("error #%d: %s\n", i, e) 35 | } 36 | return s 37 | } 38 | -------------------------------------------------------------------------------- /etcd/client/v2/discover.go: -------------------------------------------------------------------------------- 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 | 15 | package client 16 | 17 | import ( 18 | "go.etcd.io/etcd/client/pkg/v3/srv" 19 | ) 20 | 21 | // Discoverer is an interface that wraps the Discover method. 22 | type Discoverer interface { 23 | // Discover looks up the etcd servers for the domain. 24 | Discover(domain string, serviceName string) ([]string, error) 25 | } 26 | 27 | type srvDiscover struct{} 28 | 29 | // NewSRVDiscover constructs a new Discoverer that uses the stdlib to lookup SRV records. 30 | func NewSRVDiscover() Discoverer { 31 | return &srvDiscover{} 32 | } 33 | 34 | func (d *srvDiscover) Discover(domain string, serviceName string) ([]string, error) { 35 | srvs, err := srv.GetClient("etcd-client", domain, serviceName) 36 | if err != nil { 37 | return nil, err 38 | } 39 | return srvs.Endpoints, nil 40 | } 41 | -------------------------------------------------------------------------------- /etcd/client/v2/fake_transport_test.go: -------------------------------------------------------------------------------- 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 | 15 | package client 16 | 17 | import ( 18 | "errors" 19 | "net/http" 20 | ) 21 | 22 | func (t *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) { 23 | select { 24 | case resp := <-t.respchan: 25 | return resp, nil 26 | case err := <-t.errchan: 27 | return nil, err 28 | case <-t.startCancel: 29 | case <-req.Cancel: 30 | } 31 | select { 32 | // this simulates that the request is finished before cancel effects 33 | case resp := <-t.respchan: 34 | return resp, nil 35 | // wait on finishCancel to simulate taking some amount of 36 | // time while calling CancelRequest 37 | case <-t.finishCancel: 38 | return nil, errors.New("cancelled") 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /etcd/client/v2/go.mod: -------------------------------------------------------------------------------- 1 | module go.etcd.io/etcd/client/v2 2 | 3 | go 1.19 4 | 5 | require ( 6 | go.etcd.io/etcd/api/v3 v3.6.0-alpha.0 7 | go.etcd.io/etcd/client/pkg/v3 v3.6.0-alpha.0 8 | sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 9 | ) 10 | 11 | require ( 12 | github.com/coreos/go-semver v0.3.0 // indirect 13 | github.com/davecgh/go-spew v1.1.1 // indirect 14 | github.com/pmezard/go-difflib v1.0.0 // indirect 15 | github.com/stretchr/testify v1.7.2 // indirect 16 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect 17 | gopkg.in/yaml.v3 v3.0.1 // indirect 18 | ) 19 | 20 | replace ( 21 | go.etcd.io/etcd/api/v3 => ../../api 22 | go.etcd.io/etcd/client/pkg/v3 => ../pkg 23 | ) 24 | 25 | // Bad imports are sometimes causing attempts to pull that code. 26 | // This makes the error more explicit. 27 | replace ( 28 | go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY 29 | go.etcd.io/etcd/pkg/v3 => ./FORBIDDED_DEPENDENCY 30 | go.etcd.io/etcd/tests/v3 => ./FORBIDDEN_DEPENDENCY 31 | go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY 32 | ) 33 | -------------------------------------------------------------------------------- /etcd/client/v2/main_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package client_test 16 | 17 | import ( 18 | "net/http" 19 | "testing" 20 | 21 | "go.etcd.io/etcd/client/pkg/v3/testutil" 22 | ) 23 | 24 | func exampleEndpoints() []string { return nil } 25 | func exampleTransport() *http.Transport { return nil } 26 | 27 | func forUnitTestsRunInMockedContext(mocking func(), example func()) { 28 | mocking() 29 | // TODO: Call 'example' when mocking() provides realistic mocking of transport. 30 | 31 | // The real testing logic of examples gets executed 32 | // as part of ./tests/integration/client/example/... 33 | } 34 | 35 | func TestMain(m *testing.M) { 36 | testutil.MustTestMainWithLeakDetection(m) 37 | } 38 | -------------------------------------------------------------------------------- /etcd/client/v3/clientv3util/util.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package clientv3util contains utility functions derived from clientv3. 16 | package clientv3util 17 | 18 | import ( 19 | "go.etcd.io/etcd/client/v3" 20 | ) 21 | 22 | // KeyExists returns a comparison operation that evaluates to true iff the given 23 | // key exists. It does this by checking if the key `Version` is greater than 0. 24 | // It is a useful guard in transaction delete operations. 25 | func KeyExists(key string) clientv3.Cmp { 26 | return clientv3.Compare(clientv3.Version(key), ">", 0) 27 | } 28 | 29 | // KeyMissing returns a comparison operation that evaluates to true iff the 30 | // given key does not exist. 31 | func KeyMissing(key string) clientv3.Cmp { 32 | return clientv3.Compare(clientv3.Version(key), "=", 0) 33 | } 34 | -------------------------------------------------------------------------------- /etcd/client/v3/compact_op_test.go: -------------------------------------------------------------------------------- 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 | 15 | package clientv3 16 | 17 | import ( 18 | "reflect" 19 | "testing" 20 | 21 | "go.etcd.io/etcd/api/v3/etcdserverpb" 22 | ) 23 | 24 | func TestCompactOp(t *testing.T) { 25 | req1 := OpCompact(100, WithCompactPhysical()).toRequest() 26 | req2 := &etcdserverpb.CompactionRequest{Revision: 100, Physical: true} 27 | if !reflect.DeepEqual(req1, req2) { 28 | t.Fatalf("expected %+v, got %+v", req2, req1) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /etcd/client/v3/concurrency/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package concurrency implements concurrency operations on top of 16 | // etcd such as distributed locks, barriers, and elections. 17 | package concurrency 18 | -------------------------------------------------------------------------------- /etcd/client/v3/concurrency/main_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package concurrency_test 16 | 17 | import ( 18 | "testing" 19 | 20 | "go.etcd.io/etcd/client/pkg/v3/testutil" 21 | ) 22 | 23 | func exampleEndpoints() []string { return nil } 24 | 25 | func forUnitTestsRunInMockedContext(mocking func(), example func()) { 26 | mocking() 27 | // TODO: Call 'example' when mocking() provides realistic mocking of transport. 28 | 29 | // The real testing logic of examples gets executed 30 | // as part of ./tests/integration/clientv3/integration/... 31 | } 32 | 33 | func TestMain(m *testing.M) { 34 | testutil.MustTestMainWithLeakDetection(m) 35 | } 36 | -------------------------------------------------------------------------------- /etcd/client/v3/experimental/recipes/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package recipe contains experimental client-side distributed 16 | // synchronization primitives. 17 | package recipe 18 | -------------------------------------------------------------------------------- /etcd/client/v3/main_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package clientv3_test 16 | 17 | import ( 18 | "testing" 19 | "time" 20 | 21 | "go.etcd.io/etcd/client/pkg/v3/testutil" 22 | ) 23 | 24 | const ( 25 | dialTimeout = 5 * time.Second 26 | requestTimeout = 10 * time.Second 27 | ) 28 | 29 | func exampleEndpoints() []string { return nil } 30 | 31 | func forUnitTestsRunInMockedContext(mocking func(), example func()) { 32 | mocking() 33 | // TODO: Call 'example' when mocking() provides realistic mocking of transport. 34 | 35 | // The real testing logic of examples gets executed 36 | // as part of ./tests/integration/clientv3/integration/... 37 | } 38 | 39 | func TestMain(m *testing.M) { 40 | testutil.MustTestMainWithLeakDetection(m) 41 | } 42 | -------------------------------------------------------------------------------- /etcd/client/v3/mock/mockserver/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package mockserver provides mock implementations for etcdserver's server interface. 16 | package mockserver 17 | -------------------------------------------------------------------------------- /etcd/client/v3/namespace/util.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package namespace 16 | 17 | func prefixInterval(pfx string, key, end []byte) (pfxKey []byte, pfxEnd []byte) { 18 | pfxKey = make([]byte, len(pfx)+len(key)) 19 | copy(pfxKey[copy(pfxKey, pfx):], key) 20 | 21 | if len(end) == 1 && end[0] == 0 { 22 | // the edge of the keyspace 23 | pfxEnd = make([]byte, len(pfx)) 24 | copy(pfxEnd, pfx) 25 | ok := false 26 | for i := len(pfxEnd) - 1; i >= 0; i-- { 27 | if pfxEnd[i]++; pfxEnd[i] != 0 { 28 | ok = true 29 | break 30 | } 31 | } 32 | if !ok { 33 | // 0xff..ff => 0x00 34 | pfxEnd = []byte{0} 35 | } 36 | } else if len(end) >= 1 { 37 | pfxEnd = make([]byte, len(pfx)+len(end)) 38 | copy(pfxEnd[copy(pfxEnd, pfx):], end) 39 | } 40 | 41 | return pfxKey, pfxEnd 42 | } 43 | -------------------------------------------------------------------------------- /etcd/client/v3/snapshot/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package snapshot implements utilities around etcd snapshot. 16 | package snapshot 17 | -------------------------------------------------------------------------------- /etcd/client/v3/sort.go: -------------------------------------------------------------------------------- 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 | 15 | package clientv3 16 | 17 | type SortTarget int 18 | type SortOrder int 19 | 20 | const ( 21 | SortNone SortOrder = iota 22 | SortAscend 23 | SortDescend 24 | ) 25 | 26 | const ( 27 | SortByKey SortTarget = iota 28 | SortByVersion 29 | SortByCreateRevision 30 | SortByModRevision 31 | SortByValue 32 | ) 33 | 34 | type SortOption struct { 35 | Target SortTarget 36 | Order SortOrder 37 | } 38 | -------------------------------------------------------------------------------- /etcd/client/v3/utils.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package clientv3 16 | 17 | import ( 18 | "math/rand" 19 | "time" 20 | ) 21 | 22 | // jitterUp adds random jitter to the duration. 23 | // 24 | // This adds or subtracts time from the duration within a given jitter fraction. 25 | // For example for 10s and jitter 0.1, it will return a time within [9s, 11s]) 26 | // 27 | // Reference: https://godoc.org/github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils 28 | func jitterUp(duration time.Duration, jitter float64) time.Duration { 29 | multiplier := jitter * (rand.Float64()*2 - 1) 30 | return time.Duration(float64(duration) * (1 + multiplier)) 31 | } 32 | -------------------------------------------------------------------------------- /etcd/pkg/README.md: -------------------------------------------------------------------------------- 1 | pkg/ is a collection of utility packages used by etcd without being specific to etcd itself. A package belongs here 2 | only if it could possibly be moved out into its own repository in the future. 3 | -------------------------------------------------------------------------------- /etcd/pkg/adt/adt.go: -------------------------------------------------------------------------------- 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 | 15 | // Package adt implements useful abstract data types. 16 | package adt 17 | -------------------------------------------------------------------------------- /etcd/pkg/adt/example_test.go: -------------------------------------------------------------------------------- 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 | 15 | package adt_test 16 | 17 | import ( 18 | "fmt" 19 | 20 | "go.etcd.io/etcd/pkg/v3/adt" 21 | ) 22 | 23 | func Example() { 24 | ivt := adt.NewIntervalTree() 25 | ivt.Insert(adt.NewInt64Interval(1, 3), 123) 26 | ivt.Insert(adt.NewInt64Interval(9, 13), 456) 27 | ivt.Insert(adt.NewInt64Interval(7, 20), 789) 28 | 29 | rs := ivt.Stab(adt.NewInt64Point(10)) 30 | for _, v := range rs { 31 | fmt.Printf("Overlapping range: %+v\n", v) 32 | } 33 | // output: 34 | // Overlapping range: &{Ivl:{Begin:7 End:20} Val:789} 35 | // Overlapping range: &{Ivl:{Begin:9 End:13} Val:456} 36 | } 37 | -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-01-insertion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-01-insertion.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-02-delete-514.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-02-delete-514.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-03-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-03-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-04-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-04-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-05-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-05-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-06-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-06-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-07-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-07-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-08-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-08-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/adt/img/red-black-tree-09-delete-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/innovationb1ue/RedisGO/d2b271fec4fde4e57ac9a89c991fbd36b9bca06b/etcd/pkg/adt/img/red-black-tree-09-delete-11.png -------------------------------------------------------------------------------- /etcd/pkg/cobrautl/error.go: -------------------------------------------------------------------------------- 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 | 15 | package cobrautl 16 | 17 | import ( 18 | "fmt" 19 | "os" 20 | ) 21 | 22 | const ( 23 | // http://tldp.org/LDP/abs/html/exitcodes.html 24 | ExitSuccess = iota 25 | ExitError 26 | ExitBadConnection 27 | ExitInvalidInput // for txn, watch command 28 | ExitBadFeature // provided a valid flag with an unsupported value 29 | ExitInterrupted 30 | ExitIO 31 | ExitBadArgs = 128 32 | 33 | ExitServerError = 4 34 | ExitClusterNotHealthy = 5 35 | ) 36 | 37 | func ExitWithError(code int, err error) { 38 | fmt.Fprintln(os.Stderr, "Error:", err) 39 | os.Exit(code) 40 | } 41 | -------------------------------------------------------------------------------- /etcd/pkg/contention/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package contention provides facilities for detecting system contention. 16 | package contention 17 | -------------------------------------------------------------------------------- /etcd/pkg/cpuutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package cpuutil provides facilities for detecting cpu-specific features. 16 | package cpuutil 17 | -------------------------------------------------------------------------------- /etcd/pkg/cpuutil/endian.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package cpuutil 16 | 17 | import ( 18 | "encoding/binary" 19 | "unsafe" 20 | ) 21 | 22 | const intWidth = int(unsafe.Sizeof(0)) 23 | 24 | var byteOrder binary.ByteOrder 25 | 26 | // ByteOrder returns the byte order for the CPU's native endianness. 27 | func ByteOrder() binary.ByteOrder { return byteOrder } 28 | 29 | func init() { 30 | i := 0x1 31 | if v := (*[intWidth]byte)(unsafe.Pointer(&i)); v[0] == 0 { 32 | byteOrder = binary.BigEndian 33 | } else { 34 | byteOrder = binary.LittleEndian 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /etcd/pkg/crc/crc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2009 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package crc provides utility function for cyclic redundancy check 6 | // algorithms. 7 | package crc 8 | 9 | import ( 10 | "hash" 11 | "hash/crc32" 12 | ) 13 | 14 | // The size of a CRC-32 checksum in bytes. 15 | const Size = 4 16 | 17 | type digest struct { 18 | crc uint32 19 | tab *crc32.Table 20 | } 21 | 22 | // New creates a new hash.Hash32 computing the CRC-32 checksum 23 | // using the polynomial represented by the Table. 24 | // Modified by xiangli to take a prevcrc. 25 | func New(prev uint32, tab *crc32.Table) hash.Hash32 { return &digest{prev, tab} } 26 | 27 | func (d *digest) Size() int { return Size } 28 | 29 | func (d *digest) BlockSize() int { return 1 } 30 | 31 | func (d *digest) Reset() { d.crc = 0 } 32 | 33 | func (d *digest) Write(p []byte) (n int, err error) { 34 | d.crc = crc32.Update(d.crc, d.tab, p) 35 | return len(p), nil 36 | } 37 | 38 | func (d *digest) Sum32() uint32 { return d.crc } 39 | 40 | func (d *digest) Sum(in []byte) []byte { 41 | s := d.Sum32() 42 | return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) 43 | } 44 | -------------------------------------------------------------------------------- /etcd/pkg/debugutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package debugutil includes utility functions for debugging. 16 | package debugutil 17 | -------------------------------------------------------------------------------- /etcd/pkg/flags/ignored.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package flags 16 | 17 | import "go.uber.org/zap" 18 | 19 | // IgnoredFlag encapsulates a flag that may have been previously valid but is 20 | // now ignored. If an IgnoredFlag is set, a warning is printed and 21 | // operation continues. 22 | type IgnoredFlag struct { 23 | lg *zap.Logger 24 | Name string 25 | } 26 | 27 | // IsBoolFlag is defined to allow the flag to be defined without an argument 28 | func (f *IgnoredFlag) IsBoolFlag() bool { 29 | return true 30 | } 31 | 32 | func (f *IgnoredFlag) Set(s string) error { 33 | if f.lg != nil { 34 | f.lg.Warn("flag is no longer supported - ignoring", zap.String("flag-name", f.Name)) 35 | } 36 | return nil 37 | } 38 | 39 | func (f *IgnoredFlag) String() string { 40 | return "" 41 | } 42 | -------------------------------------------------------------------------------- /etcd/pkg/flags/strings_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package flags 16 | 17 | import ( 18 | "reflect" 19 | "testing" 20 | ) 21 | 22 | func TestStringsValue(t *testing.T) { 23 | tests := []struct { 24 | s string 25 | exp []string 26 | }{ 27 | {s: "a,b,c", exp: []string{"a", "b", "c"}}, 28 | {s: "a, b,c", exp: []string{"a", " b", "c"}}, 29 | {s: "", exp: []string{}}, 30 | } 31 | for i := range tests { 32 | ss := []string(*NewStringsValue(tests[i].s)) 33 | if !reflect.DeepEqual(tests[i].exp, ss) { 34 | t.Fatalf("#%d: expected %q, got %q", i, tests[i].exp, ss) 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /etcd/pkg/ioutil/reader.go: -------------------------------------------------------------------------------- 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 | 15 | // Package ioutil implements I/O utility functions. 16 | package ioutil 17 | 18 | import "io" 19 | 20 | // NewLimitedBufferReader returns a reader that reads from the given reader 21 | // but limits the amount of data returned to at most n bytes. 22 | func NewLimitedBufferReader(r io.Reader, n int) io.Reader { 23 | return &limitedBufferReader{ 24 | r: r, 25 | n: n, 26 | } 27 | } 28 | 29 | type limitedBufferReader struct { 30 | r io.Reader 31 | n int 32 | } 33 | 34 | func (r *limitedBufferReader) Read(p []byte) (n int, err error) { 35 | np := p 36 | if len(np) > r.n { 37 | np = np[:r.n] 38 | } 39 | return r.r.Read(np) 40 | } 41 | -------------------------------------------------------------------------------- /etcd/pkg/ioutil/reader_test.go: -------------------------------------------------------------------------------- 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 | 15 | package ioutil 16 | 17 | import ( 18 | "bytes" 19 | "testing" 20 | ) 21 | 22 | func TestLimitedBufferReaderRead(t *testing.T) { 23 | buf := bytes.NewBuffer(make([]byte, 10)) 24 | ln := 1 25 | lr := NewLimitedBufferReader(buf, ln) 26 | n, err := lr.Read(make([]byte, 10)) 27 | if err != nil { 28 | t.Fatalf("unexpected read error: %v", err) 29 | } 30 | if n != ln { 31 | t.Errorf("len(data read) = %d, want %d", n, ln) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /etcd/pkg/ioutil/util.go: -------------------------------------------------------------------------------- 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 | 15 | package ioutil 16 | 17 | import ( 18 | "io" 19 | "os" 20 | 21 | "go.etcd.io/etcd/client/pkg/v3/fileutil" 22 | ) 23 | 24 | // WriteAndSyncFile behaves just like ioutil.WriteFile in the standard library, 25 | // but calls Sync before closing the file. WriteAndSyncFile guarantees the data 26 | // is synced if there is no error returned. 27 | func WriteAndSyncFile(filename string, data []byte, perm os.FileMode) error { 28 | f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) 29 | if err != nil { 30 | return err 31 | } 32 | n, err := f.Write(data) 33 | if err == nil && n < len(data) { 34 | err = io.ErrShortWrite 35 | } 36 | if err == nil { 37 | err = fileutil.Fsync(f) 38 | } 39 | if err1 := f.Close(); err == nil { 40 | err = err1 41 | } 42 | return err 43 | } 44 | -------------------------------------------------------------------------------- /etcd/pkg/netutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package netutil implements network-related utility functions. 16 | package netutil 17 | -------------------------------------------------------------------------------- /etcd/pkg/netutil/routes.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !linux 16 | // +build !linux 17 | 18 | package netutil 19 | 20 | import ( 21 | "fmt" 22 | "runtime" 23 | ) 24 | 25 | // GetDefaultHost fetches the a resolvable name that corresponds 26 | // to the machine's default routable interface 27 | func GetDefaultHost() (string, error) { 28 | return "", fmt.Errorf("default host not supported on %s_%s", runtime.GOOS, runtime.GOARCH) 29 | } 30 | 31 | // GetDefaultInterfaces fetches the device name of default routable interface. 32 | func GetDefaultInterfaces() (map[string]uint8, error) { 33 | return nil, fmt.Errorf("default host not supported on %s_%s", runtime.GOOS, runtime.GOARCH) 34 | } 35 | -------------------------------------------------------------------------------- /etcd/pkg/netutil/routes_linux_test.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build linux 16 | // +build linux 17 | 18 | package netutil 19 | 20 | import "testing" 21 | 22 | func TestGetDefaultInterface(t *testing.T) { 23 | ifc, err := GetDefaultInterfaces() 24 | if err != nil { 25 | t.Fatal(err) 26 | } 27 | t.Logf("default network interfaces: %+v\n", ifc) 28 | } 29 | 30 | func TestGetDefaultHost(t *testing.T) { 31 | ip, err := GetDefaultHost() 32 | if err != nil { 33 | t.Fatal(err) 34 | } 35 | t.Logf("default ip: %v", ip) 36 | } 37 | -------------------------------------------------------------------------------- /etcd/pkg/osutil/interrupt_windows.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build windows 16 | // +build windows 17 | 18 | package osutil 19 | 20 | import ( 21 | "os" 22 | 23 | "go.uber.org/zap" 24 | ) 25 | 26 | type InterruptHandler func() 27 | 28 | // RegisterInterruptHandler is a no-op on windows 29 | func RegisterInterruptHandler(h InterruptHandler) {} 30 | 31 | // HandleInterrupts is a no-op on windows 32 | func HandleInterrupts(*zap.Logger) {} 33 | 34 | // Exit calls os.Exit 35 | func Exit(code int) { 36 | os.Exit(code) 37 | } 38 | -------------------------------------------------------------------------------- /etcd/pkg/osutil/osutil.go: -------------------------------------------------------------------------------- 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 | 15 | // Package osutil implements operating system-related utility functions. 16 | package osutil 17 | 18 | import ( 19 | "os" 20 | "strings" 21 | ) 22 | 23 | var ( 24 | // support to override setting SIG_DFL so tests don't terminate early 25 | setDflSignal = dflSignal 26 | ) 27 | 28 | func Unsetenv(key string) error { 29 | envs := os.Environ() 30 | os.Clearenv() 31 | for _, e := range envs { 32 | strs := strings.SplitN(e, "=", 2) 33 | if strs[0] == key { 34 | continue 35 | } 36 | if err := os.Setenv(strs[0], strs[1]); err != nil { 37 | return err 38 | } 39 | } 40 | return nil 41 | } 42 | -------------------------------------------------------------------------------- /etcd/pkg/osutil/signal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | //go:build !linux || cov 16 | // +build !linux cov 17 | 18 | package osutil 19 | 20 | import "syscall" 21 | 22 | func dflSignal(sig syscall.Signal) { /* nop */ } 23 | -------------------------------------------------------------------------------- /etcd/pkg/osutil/signal_linux.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | //go:build linux && !cov 16 | // +build linux,!cov 17 | 18 | package osutil 19 | 20 | import ( 21 | "syscall" 22 | "unsafe" 23 | ) 24 | 25 | // dflSignal sets the given signal to SIG_DFL 26 | func dflSignal(sig syscall.Signal) { 27 | // clearing out the sigact sets the signal to SIG_DFL 28 | var sigactBuf [32]uint64 29 | ptr := unsafe.Pointer(&sigactBuf) 30 | syscall.Syscall6(uintptr(syscall.SYS_RT_SIGACTION), uintptr(sig), uintptr(ptr), 0, 8, 0, 0) 31 | } 32 | -------------------------------------------------------------------------------- /etcd/pkg/proxy/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package proxy implements proxy servers for network fault testing. 16 | package proxy 17 | -------------------------------------------------------------------------------- /etcd/pkg/proxy/fixtures/ca-csr.json: -------------------------------------------------------------------------------- 1 | { 2 | "key": { 3 | "algo": "rsa", 4 | "size": 2048 5 | }, 6 | "names": [ 7 | { 8 | "O": "etcd", 9 | "OU": "etcd Security", 10 | "L": "San Francisco", 11 | "ST": "California", 12 | "C": "USA" 13 | } 14 | ], 15 | "CN": "ca", 16 | "ca": { 17 | "expiry": "87600h" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /etcd/pkg/proxy/fixtures/ca.crt: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDsTCCApmgAwIBAgIUZzOo4zcHY/nEXY1PD8A7povXlWUwDQYJKoZIhvcNAQEL 3 | BQAwbzEMMAoGA1UEBhMDVVNBMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH 4 | Ew1TYW4gRnJhbmNpc2NvMQ0wCwYDVQQKEwRldGNkMRYwFAYDVQQLEw1ldGNkIFNl 5 | Y3VyaXR5MQswCQYDVQQDEwJjYTAeFw0xODAxMDIxNjQxMDBaFw0yNzEyMzExNjQx 6 | MDBaMG8xDDAKBgNVBAYTA1VTQTETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UE 7 | BxMNU2FuIEZyYW5jaXNjbzENMAsGA1UEChMEZXRjZDEWMBQGA1UECxMNZXRjZCBT 8 | ZWN1cml0eTELMAkGA1UEAxMCY2EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK 9 | AoIBAQDD4Ys48LDWGyojj3Rcr6fnESY+UycaaGoTXADWLPmm+sQR3KcsJxF4054S 10 | d2G+NBfJHZvTHhVqOeqZxNtoqgje4paY2A5TbWBdV+xoGfbakwwngiX1yeF1I54k 11 | KH19zb8rBKAm7xixO60hE2CIYzMuw9lDkwoHpI6/PJdy7jwtytbo2Oac512JiO9Y 12 | dHp9dr3mrCzoKEBRtL1asRKfzp6gBC5rIw5T4jrq37feerV4pDEJX7fvexxVocVm 13 | tT4bmMq3Ap6OFFAzmE/ITI8pXvFaOd9lyebNXQmrreKJLUfEIZa6JulLCYxfkJ8z 14 | +CcNLyn6ZXNMaIZ8G9Hm6VRdRi8/AgMBAAGjRTBDMA4GA1UdDwEB/wQEAwIBBjAS 15 | BgNVHRMBAf8ECDAGAQH/AgECMB0GA1UdDgQWBBRDLNYEX8XI7nM53k1rUR+mpTjQ 16 | NTANBgkqhkiG9w0BAQsFAAOCAQEACDe3Fa1KE/rvVtyCLW/IBfKV01NShFTsb6x8 17 | GrPEQ6NJLZQ2MzdyJgAF2a/nZ9KVgrhGXoyoZBCKP9Dd/JDzSSZcBztfNK8dRv2A 18 | XHBBF6tZ19I+XY9c7/CfhJ2CEYJpeN9r3GKSqV+njkmg8n/On2BTlFsij88plK8H 19 | ORyemc1nQI+ARPSu2r3rJbYa4yI2U6w4L4BTCVImg3bX50GImmXGlwvnJMFik1FX 20 | +0hdfetRxxMZ1pm2Uy6099KkULnSKabZGwRiBUHQJYh0EeuAOQ4a6MG5DRkURWNs 21 | dInjPOLY9/7S5DQKwz/NtqXA8EEymZosHxpiRp+zzKB4XaV9Ig== 22 | -----END CERTIFICATE----- 23 | -------------------------------------------------------------------------------- /etcd/pkg/proxy/fixtures/gencert.json: -------------------------------------------------------------------------------- 1 | { 2 | "signing": { 3 | "default": { 4 | "usages": [ 5 | "signing", 6 | "key encipherment", 7 | "server auth", 8 | "client auth" 9 | ], 10 | "expiry": "87600h" 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /etcd/pkg/proxy/fixtures/gencerts.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if ! [[ "$0" =~ "./gencerts.sh" ]]; then 4 | echo "must be run from 'fixtures'" 5 | exit 255 6 | fi 7 | 8 | if ! which cfssl; then 9 | echo "cfssl is not installed" 10 | exit 255 11 | fi 12 | 13 | cfssl gencert --initca=true ./ca-csr.json | cfssljson --bare ./ca 14 | mv ca.pem ca.crt 15 | openssl x509 -in ca.crt -noout -text 16 | 17 | # generate DNS: localhost, IP: 127.0.0.1, CN: example.com certificates 18 | cfssl gencert \ 19 | --ca ./ca.crt \ 20 | --ca-key ./ca-key.pem \ 21 | --config ./gencert.json \ 22 | ./server-ca-csr.json | cfssljson --bare ./server 23 | mv server.pem server.crt 24 | mv server-key.pem server.key.insecure 25 | 26 | rm -f *.csr *.pem *.stderr *.txt 27 | -------------------------------------------------------------------------------- /etcd/pkg/proxy/fixtures/server-ca-csr.json: -------------------------------------------------------------------------------- 1 | { 2 | "key": { 3 | "algo": "rsa", 4 | "size": 2048 5 | }, 6 | "names": [ 7 | { 8 | "O": "etcd", 9 | "OU": "etcd Security", 10 | "L": "San Francisco", 11 | "ST": "California", 12 | "C": "USA" 13 | } 14 | ], 15 | "CN": "example.com", 16 | "hosts": [ 17 | "127.0.0.1", 18 | "localhost" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /etcd/pkg/report/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package report generates human-readable benchmark reports. 16 | package report 17 | -------------------------------------------------------------------------------- /etcd/pkg/runtime/fds_linux.go: -------------------------------------------------------------------------------- 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 | 15 | // Package runtime implements utility functions for runtime systems. 16 | package runtime 17 | 18 | import ( 19 | "os" 20 | "syscall" 21 | ) 22 | 23 | func FDLimit() (uint64, error) { 24 | var rlimit syscall.Rlimit 25 | if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil { 26 | return 0, err 27 | } 28 | return rlimit.Cur, nil 29 | } 30 | 31 | func FDUsage() (uint64, error) { 32 | return countFiles("/proc/self/fd") 33 | } 34 | 35 | // countFiles reads the directory named by dirname and returns the count. 36 | func countFiles(dirname string) (uint64, error) { 37 | f, err := os.Open(dirname) 38 | if err != nil { 39 | return 0, err 40 | } 41 | list, err := f.Readdirnames(-1) 42 | f.Close() 43 | if err != nil { 44 | return 0, err 45 | } 46 | return uint64(len(list)), nil 47 | } 48 | -------------------------------------------------------------------------------- /etcd/pkg/runtime/fds_other.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !linux 16 | // +build !linux 17 | 18 | package runtime 19 | 20 | import ( 21 | "fmt" 22 | "runtime" 23 | ) 24 | 25 | func FDLimit() (uint64, error) { 26 | return 0, fmt.Errorf("cannot get FDLimit on %s", runtime.GOOS) 27 | } 28 | 29 | func FDUsage() (uint64, error) { 30 | return 0, fmt.Errorf("cannot get FDUsage on %s", runtime.GOOS) 31 | } 32 | -------------------------------------------------------------------------------- /etcd/pkg/schedule/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package schedule provides mechanisms and policies for scheduling units of work. 16 | package schedule 17 | -------------------------------------------------------------------------------- /etcd/pkg/stringutil/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | // Package stringutil exports string utility functions. 16 | package stringutil 17 | -------------------------------------------------------------------------------- /etcd/pkg/stringutil/rand_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package stringutil 16 | 17 | import ( 18 | "fmt" 19 | "testing" 20 | ) 21 | 22 | func TestUniqueStrings(t *testing.T) { 23 | ss := UniqueStrings(10, 50) 24 | for i := 1; i < len(ss); i++ { 25 | if ss[i-1] == ss[i] { 26 | t.Fatalf("ss[i-1] %q == ss[i] %q", ss[i-1], ss[i]) 27 | } 28 | } 29 | fmt.Println(ss) 30 | } 31 | -------------------------------------------------------------------------------- /etcd/raft/OWNERS: -------------------------------------------------------------------------------- 1 | approvers: 2 | - heyitsanthony 3 | - philips 4 | - fanminshi 5 | - gyuho 6 | - mitake 7 | - jpbetz 8 | - xiang90 9 | - bdarnell 10 | reviewers: 11 | - heyitsanthony 12 | - philips 13 | - fanminshi 14 | - gyuho 15 | - mitake 16 | - jpbetz 17 | - xiang90 18 | - bdarnell 19 | - tschottdorf 20 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/joint_autoleave.txt: -------------------------------------------------------------------------------- 1 | # Test the autoleave argument to EnterJoint. It defaults to false in the 2 | # datadriven tests. The flag has no associated semantics in this package, 3 | # it is simply passed through. 4 | simple 5 | v1 6 | ---- 7 | voters=(1) 8 | 1: StateProbe match=0 next=0 9 | 10 | # Autoleave is reflected in the config. 11 | enter-joint autoleave=true 12 | v2 v3 13 | ---- 14 | voters=(1 2 3)&&(1) autoleave 15 | 1: StateProbe match=0 next=0 16 | 2: StateProbe match=0 next=1 17 | 3: StateProbe match=0 next=1 18 | 19 | # Can't enter-joint twice, even if autoleave changes. 20 | enter-joint autoleave=false 21 | ---- 22 | config is already joint 23 | 24 | leave-joint 25 | ---- 26 | voters=(1 2 3) 27 | 1: StateProbe match=0 next=0 28 | 2: StateProbe match=0 next=1 29 | 3: StateProbe match=0 next=1 30 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/joint_idempotency.txt: -------------------------------------------------------------------------------- 1 | # Verify that operations upon entering the joint state are idempotent, i.e. 2 | # removing an absent node is fine, etc. 3 | 4 | simple 5 | v1 6 | ---- 7 | voters=(1) 8 | 1: StateProbe match=0 next=0 9 | 10 | enter-joint 11 | r1 r2 r9 v2 v3 v4 v2 v3 v4 l2 l2 r4 r4 l1 l1 12 | ---- 13 | voters=(3)&&(1) learners=(2) learners_next=(1) 14 | 1: StateProbe match=0 next=0 15 | 2: StateProbe match=0 next=1 learner 16 | 3: StateProbe match=0 next=1 17 | 18 | leave-joint 19 | ---- 20 | voters=(3) learners=(1 2) 21 | 1: StateProbe match=0 next=0 learner 22 | 2: StateProbe match=0 next=1 learner 23 | 3: StateProbe match=0 next=1 24 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/joint_learners_next.txt: -------------------------------------------------------------------------------- 1 | # Verify that when a voter is demoted in a joint config, it will show up in 2 | # learners_next until the joint config is left, and only then will the progress 3 | # turn into that of a learner, without resetting the progress. Note that this 4 | # last fact is verified by `next`, which can tell us which "round" the progress 5 | # was originally created in. 6 | 7 | simple 8 | v1 9 | ---- 10 | voters=(1) 11 | 1: StateProbe match=0 next=0 12 | 13 | enter-joint 14 | v2 l1 15 | ---- 16 | voters=(2)&&(1) learners_next=(1) 17 | 1: StateProbe match=0 next=0 18 | 2: StateProbe match=0 next=1 19 | 20 | leave-joint 21 | ---- 22 | voters=(2) learners=(1) 23 | 1: StateProbe match=0 next=0 learner 24 | 2: StateProbe match=0 next=1 25 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/simple_idempotency.txt: -------------------------------------------------------------------------------- 1 | simple 2 | v1 3 | ---- 4 | voters=(1) 5 | 1: StateProbe match=0 next=0 6 | 7 | simple 8 | v1 9 | ---- 10 | voters=(1) 11 | 1: StateProbe match=0 next=0 12 | 13 | simple 14 | v2 15 | ---- 16 | voters=(1 2) 17 | 1: StateProbe match=0 next=0 18 | 2: StateProbe match=0 next=2 19 | 20 | simple 21 | l1 22 | ---- 23 | voters=(2) learners=(1) 24 | 1: StateProbe match=0 next=0 learner 25 | 2: StateProbe match=0 next=2 26 | 27 | simple 28 | l1 29 | ---- 30 | voters=(2) learners=(1) 31 | 1: StateProbe match=0 next=0 learner 32 | 2: StateProbe match=0 next=2 33 | 34 | simple 35 | r1 36 | ---- 37 | voters=(2) 38 | 2: StateProbe match=0 next=2 39 | 40 | simple 41 | r1 42 | ---- 43 | voters=(2) 44 | 2: StateProbe match=0 next=2 45 | 46 | simple 47 | v3 48 | ---- 49 | voters=(2 3) 50 | 2: StateProbe match=0 next=2 51 | 3: StateProbe match=0 next=7 52 | 53 | simple 54 | r3 55 | ---- 56 | voters=(2) 57 | 2: StateProbe match=0 next=2 58 | 59 | simple 60 | r3 61 | ---- 62 | voters=(2) 63 | 2: StateProbe match=0 next=2 64 | 65 | simple 66 | r4 67 | ---- 68 | voters=(2) 69 | 2: StateProbe match=0 next=2 70 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/simple_promote_demote.txt: -------------------------------------------------------------------------------- 1 | # Set up three voters for this test. 2 | 3 | simple 4 | v1 5 | ---- 6 | voters=(1) 7 | 1: StateProbe match=0 next=0 8 | 9 | simple 10 | v2 11 | ---- 12 | voters=(1 2) 13 | 1: StateProbe match=0 next=0 14 | 2: StateProbe match=0 next=1 15 | 16 | simple 17 | v3 18 | ---- 19 | voters=(1 2 3) 20 | 1: StateProbe match=0 next=0 21 | 2: StateProbe match=0 next=1 22 | 3: StateProbe match=0 next=2 23 | 24 | # Can atomically demote and promote without a hitch. 25 | # This is pointless, but possible. 26 | simple 27 | l1 v1 28 | ---- 29 | voters=(1 2 3) 30 | 1: StateProbe match=0 next=0 31 | 2: StateProbe match=0 next=1 32 | 3: StateProbe match=0 next=2 33 | 34 | # Can demote a voter. 35 | simple 36 | l2 37 | ---- 38 | voters=(1 3) learners=(2) 39 | 1: StateProbe match=0 next=0 40 | 2: StateProbe match=0 next=1 learner 41 | 3: StateProbe match=0 next=2 42 | 43 | # Can atomically promote and demote the same voter. 44 | # This is pointless, but possible. 45 | simple 46 | v2 l2 47 | ---- 48 | voters=(1 3) learners=(2) 49 | 1: StateProbe match=0 next=0 50 | 2: StateProbe match=0 next=1 learner 51 | 3: StateProbe match=0 next=2 52 | 53 | # Can promote a voter. 54 | simple 55 | v2 56 | ---- 57 | voters=(1 2 3) 58 | 1: StateProbe match=0 next=0 59 | 2: StateProbe match=0 next=1 60 | 3: StateProbe match=0 next=2 61 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/simple_safety.txt: -------------------------------------------------------------------------------- 1 | simple 2 | l1 3 | ---- 4 | removed all voters 5 | 6 | simple 7 | v1 8 | ---- 9 | voters=(1) 10 | 1: StateProbe match=0 next=1 11 | 12 | simple 13 | v2 l3 14 | ---- 15 | voters=(1 2) learners=(3) 16 | 1: StateProbe match=0 next=1 17 | 2: StateProbe match=0 next=2 18 | 3: StateProbe match=0 next=2 learner 19 | 20 | simple 21 | r1 v5 22 | ---- 23 | more than one voter changed without entering joint config 24 | 25 | simple 26 | r1 r2 27 | ---- 28 | removed all voters 29 | 30 | simple 31 | v3 v4 32 | ---- 33 | more than one voter changed without entering joint config 34 | 35 | simple 36 | l1 v5 37 | ---- 38 | more than one voter changed without entering joint config 39 | 40 | simple 41 | l1 l2 42 | ---- 43 | removed all voters 44 | 45 | simple 46 | l2 l3 l4 l5 47 | ---- 48 | voters=(1) learners=(2 3 4 5) 49 | 1: StateProbe match=0 next=1 50 | 2: StateProbe match=0 next=2 learner 51 | 3: StateProbe match=0 next=2 learner 52 | 4: StateProbe match=0 next=8 learner 53 | 5: StateProbe match=0 next=8 learner 54 | 55 | simple 56 | r1 57 | ---- 58 | removed all voters 59 | 60 | simple 61 | r2 r3 r4 r5 62 | ---- 63 | voters=(1) 64 | 1: StateProbe match=0 next=1 65 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/update.txt: -------------------------------------------------------------------------------- 1 | # Nobody cares about ConfChangeUpdateNode, but at least use it once. It is used 2 | # by etcd as a convenient way to pass a blob through their conf change machinery 3 | # that updates information tracked outside of raft. 4 | 5 | simple 6 | v1 7 | ---- 8 | voters=(1) 9 | 1: StateProbe match=0 next=0 10 | 11 | simple 12 | v2 u1 13 | ---- 14 | voters=(1 2) 15 | 1: StateProbe match=0 next=0 16 | 2: StateProbe match=0 next=1 17 | 18 | simple 19 | u1 u2 u3 u1 u2 u3 20 | ---- 21 | voters=(1 2) 22 | 1: StateProbe match=0 next=0 23 | 2: StateProbe match=0 next=1 24 | -------------------------------------------------------------------------------- /etcd/raft/confchange/testdata/zero.txt: -------------------------------------------------------------------------------- 1 | # NodeID zero is ignored. 2 | simple 3 | v1 r0 v0 l0 4 | ---- 5 | voters=(1) 6 | 1: StateProbe match=0 next=0 7 | -------------------------------------------------------------------------------- /etcd/raft/example_test.go: -------------------------------------------------------------------------------- 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 | 15 | package raft 16 | 17 | import ( 18 | pb "go.etcd.io/etcd/raft/v3/raftpb" 19 | ) 20 | 21 | func applyToStore(ents []pb.Entry) {} 22 | func sendMessages(msgs []pb.Message) {} 23 | func saveStateToDisk(st pb.HardState) {} 24 | func saveToDisk(ents []pb.Entry) {} 25 | 26 | func ExampleNode() { 27 | c := &Config{} 28 | n := StartNode(c, nil) 29 | defer n.Stop() 30 | 31 | // stuff to n happens in other goroutines 32 | 33 | // the last known state 34 | var prev pb.HardState 35 | for { 36 | // Ready blocks until there is new state ready. 37 | rd := <-n.Ready() 38 | if !isHardStateEqual(prev, rd.HardState) { 39 | saveStateToDisk(rd.HardState) 40 | prev = rd.HardState 41 | } 42 | 43 | saveToDisk(rd.Entries) 44 | go applyToStore(rd.CommittedEntries) 45 | sendMessages(rd.Messages) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /etcd/raft/go.mod: -------------------------------------------------------------------------------- 1 | module go.etcd.io/etcd/raft/v3 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 // indirect 7 | github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5 8 | github.com/gogo/protobuf v1.3.2 9 | github.com/golang/protobuf v1.5.2 10 | github.com/pkg/errors v0.9.1 // indirect 11 | go.etcd.io/etcd/api/v3 v3.6.0-alpha.0 12 | go.etcd.io/etcd/client/pkg/v3 v3.6.0-alpha.0 13 | ) 14 | 15 | require ( 16 | github.com/cockroachdb/errors v1.2.4 // indirect 17 | github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect 18 | github.com/davecgh/go-spew v1.1.1 // indirect 19 | github.com/getsentry/raven-go v0.2.0 // indirect 20 | github.com/google/go-cmp v0.5.8 // indirect 21 | github.com/kr/pretty v0.2.1 // indirect 22 | github.com/pmezard/go-difflib v1.0.0 // indirect 23 | github.com/stretchr/testify v1.7.2 // indirect 24 | google.golang.org/protobuf v1.27.1 // indirect 25 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect 26 | gopkg.in/yaml.v3 v3.0.1 // indirect 27 | ) 28 | 29 | // Bad imports are sometimes causing attempts to pull that code. 30 | // This makes the error more explicit. 31 | replace go.etcd.io/etcd => ./FORBIDDEN_DEPENDENCY 32 | 33 | replace go.etcd.io/etcd/v3 => ./FORBIDDEN_DEPENDENCY 34 | 35 | replace go.etcd.io/etcd/client/pkg/v3 => ../client/pkg 36 | 37 | replace go.etcd.io/etcd/api/v3 => ../api 38 | -------------------------------------------------------------------------------- /etcd/raft/interaction_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package raft_test 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/cockroachdb/datadriven" 21 | "go.etcd.io/etcd/raft/v3/rafttest" 22 | ) 23 | 24 | func TestInteraction(t *testing.T) { 25 | // NB: if this test fails, run `go test ./raft -rewrite` and inspect the 26 | // diff. Only commit the changes if you understand what caused them and if 27 | // they are desired. 28 | datadriven.Walk(t, "testdata", func(t *testing.T, path string) { 29 | env := rafttest.NewInteractionEnv(nil) 30 | datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string { 31 | return env.Handle(t, *d) 32 | }) 33 | }) 34 | } 35 | -------------------------------------------------------------------------------- /etcd/raft/node_bench_test.go: -------------------------------------------------------------------------------- 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 | 15 | package raft 16 | 17 | import ( 18 | "context" 19 | "testing" 20 | "time" 21 | ) 22 | 23 | func BenchmarkOneNode(b *testing.B) { 24 | ctx, cancel := context.WithCancel(context.Background()) 25 | defer cancel() 26 | 27 | s := newTestMemoryStorage(withPeers(1)) 28 | rn := newTestRawNode(1, 10, 1, s) 29 | n := newNode(rn) 30 | go n.run() 31 | 32 | defer n.Stop() 33 | 34 | n.Campaign(ctx) 35 | go func() { 36 | for i := 0; i < b.N; i++ { 37 | n.Propose(ctx, []byte("foo")) 38 | } 39 | }() 40 | 41 | for { 42 | rd := <-n.Ready() 43 | s.Append(rd.Entries) 44 | // a reasonable disk sync latency 45 | time.Sleep(1 * time.Millisecond) 46 | n.Advance() 47 | if rd.HardState.Commit == uint64(b.N+1) { 48 | return 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /etcd/raft/quorum/bench_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package quorum 16 | 17 | import ( 18 | "fmt" 19 | "math" 20 | "math/rand" 21 | "testing" 22 | ) 23 | 24 | func BenchmarkMajorityConfig_CommittedIndex(b *testing.B) { 25 | // go test -run - -bench . -benchmem ./raft/quorum 26 | for _, n := range []int{1, 3, 5, 7, 9, 11} { 27 | b.Run(fmt.Sprintf("voters=%d", n), func(b *testing.B) { 28 | c := MajorityConfig{} 29 | l := mapAckIndexer{} 30 | for i := uint64(0); i < uint64(n); i++ { 31 | c[i+1] = struct{}{} 32 | l[i+1] = Index(rand.Int63n(math.MaxInt64)) 33 | } 34 | 35 | for i := 0; i < b.N; i++ { 36 | _ = c.CommittedIndex(l) 37 | } 38 | }) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /etcd/raft/quorum/voteresult_string.go: -------------------------------------------------------------------------------- 1 | // Code generated by "stringer -type=VoteResult"; DO NOT EDIT. 2 | 3 | package quorum 4 | 5 | import "strconv" 6 | 7 | func _() { 8 | // An "invalid array index" compiler error signifies that the constant values have changed. 9 | // Re-run the stringer command to generate them again. 10 | var x [1]struct{} 11 | _ = x[VotePending-1] 12 | _ = x[VoteLost-2] 13 | _ = x[VoteWon-3] 14 | } 15 | 16 | const _VoteResult_name = "VotePendingVoteLostVoteWon" 17 | 18 | var _VoteResult_index = [...]uint8{0, 11, 19, 26} 19 | 20 | func (i VoteResult) String() string { 21 | i -= 1 22 | if i >= VoteResult(len(_VoteResult_index)-1) { 23 | return "VoteResult(" + strconv.FormatInt(int64(i+1), 10) + ")" 24 | } 25 | return _VoteResult_name[_VoteResult_index[i]:_VoteResult_index[i+1]] 26 | } 27 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package rafttest provides functional tests for etcd's raft implementation. 16 | package rafttest 17 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_campaign.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package rafttest 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/cockroachdb/datadriven" 21 | ) 22 | 23 | func (env *InteractionEnv) handleCampaign(t *testing.T, d datadriven.TestData) error { 24 | idx := firstAsNodeIdx(t, d) 25 | return env.Campaign(t, idx) 26 | } 27 | 28 | // Campaign the node at the given index. 29 | func (env *InteractionEnv) Campaign(t *testing.T, idx int) error { 30 | return env.Nodes[idx].Campaign() 31 | } 32 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_compact.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package rafttest 16 | 17 | import ( 18 | "strconv" 19 | "testing" 20 | 21 | "github.com/cockroachdb/datadriven" 22 | ) 23 | 24 | func (env *InteractionEnv) handleCompact(t *testing.T, d datadriven.TestData) error { 25 | idx := firstAsNodeIdx(t, d) 26 | newFirstIndex, err := strconv.ParseUint(d.CmdArgs[1].Key, 10, 64) 27 | if err != nil { 28 | return err 29 | } 30 | return env.Compact(idx, newFirstIndex) 31 | } 32 | 33 | // Compact truncates the log on the node at index idx so that the supplied new 34 | // first index results. 35 | func (env *InteractionEnv) Compact(idx int, newFirstIndex uint64) error { 36 | if err := env.Nodes[idx].Compact(newFirstIndex); err != nil { 37 | return err 38 | } 39 | return env.RaftLog(idx) 40 | } 41 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_log_level.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package rafttest 16 | 17 | import ( 18 | "fmt" 19 | "strings" 20 | "testing" 21 | 22 | "github.com/cockroachdb/datadriven" 23 | ) 24 | 25 | func (env *InteractionEnv) handleLogLevel(t *testing.T, d datadriven.TestData) error { 26 | return env.LogLevel(d.CmdArgs[0].Key) 27 | } 28 | 29 | func (env *InteractionEnv) LogLevel(name string) error { 30 | for i, s := range lvlNames { 31 | if strings.EqualFold(s, name) { 32 | env.Output.Lvl = i 33 | return nil 34 | } 35 | } 36 | return fmt.Errorf("log levels must be either of %v", lvlNames) 37 | } 38 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_propose.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package rafttest 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/cockroachdb/datadriven" 21 | ) 22 | 23 | func (env *InteractionEnv) handlePropose(t *testing.T, d datadriven.TestData) error { 24 | idx := firstAsNodeIdx(t, d) 25 | if len(d.CmdArgs) != 2 || len(d.CmdArgs[1].Vals) > 0 { 26 | t.Fatalf("expected exactly one key with no vals: %+v", d.CmdArgs[1:]) 27 | } 28 | return env.Propose(idx, []byte(d.CmdArgs[1].Key)) 29 | } 30 | 31 | // Propose a regular entry. 32 | func (env *InteractionEnv) Propose(idx int, data []byte) error { 33 | return env.Nodes[idx].Propose(data) 34 | } 35 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_status.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package rafttest 16 | 17 | import ( 18 | "fmt" 19 | "testing" 20 | 21 | "github.com/cockroachdb/datadriven" 22 | "go.etcd.io/etcd/raft/v3/tracker" 23 | ) 24 | 25 | func (env *InteractionEnv) handleStatus(t *testing.T, d datadriven.TestData) error { 26 | idx := firstAsNodeIdx(t, d) 27 | return env.Status(idx) 28 | } 29 | 30 | // Status pretty-prints the raft status for the node at the given index to the output 31 | // buffer. 32 | func (env *InteractionEnv) Status(idx int) error { 33 | // TODO(tbg): actually print the full status. 34 | st := env.Nodes[idx].Status() 35 | m := tracker.ProgressMap{} 36 | for id, pr := range st.Progress { 37 | pr := pr // loop-local copy 38 | m[id] = &pr 39 | } 40 | fmt.Fprint(env.Output, m) 41 | return nil 42 | } 43 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_tick_heartbeat.go: -------------------------------------------------------------------------------- 1 | // Copyright 2019 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 | 15 | package rafttest 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/cockroachdb/datadriven" 21 | ) 22 | 23 | func (env *InteractionEnv) handleTickHeartbeat(t *testing.T, d datadriven.TestData) error { 24 | idx := firstAsNodeIdx(t, d) 25 | return env.Tick(idx, env.Nodes[idx].Config.HeartbeatTick) 26 | } 27 | 28 | // Tick the node at the given index the given number of times. 29 | func (env *InteractionEnv) Tick(idx int, num int) error { 30 | for i := 0; i < num; i++ { 31 | env.Nodes[idx].Tick() 32 | } 33 | return nil 34 | } 35 | -------------------------------------------------------------------------------- /etcd/raft/rafttest/interaction_env_handler_transfer_leadership.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package rafttest 16 | 17 | import ( 18 | "testing" 19 | 20 | "github.com/cockroachdb/datadriven" 21 | ) 22 | 23 | func (env *InteractionEnv) handleTransferLeadership(t *testing.T, d datadriven.TestData) error { 24 | var from, to uint64 25 | d.ScanArgs(t, "from", &from) 26 | d.ScanArgs(t, "to", &to) 27 | if from == 0 || from > uint64(len(env.Nodes)) { 28 | t.Fatalf(`expected valid "from" argument`) 29 | } 30 | if to == 0 || to > uint64(len(env.Nodes)) { 31 | t.Fatalf(`expected valid "to" argument`) 32 | } 33 | return env.transferLeadership(from, to) 34 | } 35 | 36 | // Initiate leadership transfer. 37 | func (env *InteractionEnv) transferLeadership(from, to uint64) error { 38 | fromIdx := from - 1 39 | env.Nodes[fromIdx].TransferLeader(to) 40 | return nil 41 | } 42 | -------------------------------------------------------------------------------- /etcd/raft/testdata/single_node.txt: -------------------------------------------------------------------------------- 1 | log-level info 2 | ---- 3 | ok 4 | 5 | add-nodes 1 voters=(1) index=3 6 | ---- 7 | INFO 1 switched to configuration voters=(1) 8 | INFO 1 became follower at term 0 9 | INFO newRaft 1 [peers: [1], term: 0, commit: 3, applied: 3, lastindex: 3, lastterm: 1] 10 | 11 | campaign 1 12 | ---- 13 | INFO 1 is starting a new election at term 0 14 | INFO 1 became candidate at term 1 15 | INFO 1 received MsgVoteResp from 1 at term 1 16 | INFO 1 became leader at term 1 17 | 18 | stabilize 19 | ---- 20 | > 1 handling Ready 21 | Ready MustSync=true: 22 | Lead:1 State:StateLeader 23 | HardState Term:1 Vote:1 Commit:3 24 | Entries: 25 | 1/4 EntryNormal "" 26 | > 1 handling Ready 27 | Ready MustSync=false: 28 | HardState Term:1 Vote:1 Commit:4 29 | CommittedEntries: 30 | 1/4 EntryNormal "" 31 | -------------------------------------------------------------------------------- /etcd/server/auth/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package auth provides client role authentication for accessing keys in etcd. 16 | package auth 17 | -------------------------------------------------------------------------------- /etcd/server/auth/main_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package auth 6 | 7 | import ( 8 | "testing" 9 | 10 | "go.etcd.io/etcd/client/pkg/v3/testutil" 11 | ) 12 | 13 | func TestMain(m *testing.M) { 14 | testutil.MustTestMainWithLeakDetection(m) 15 | } 16 | -------------------------------------------------------------------------------- /etcd/server/auth/metrics.go: -------------------------------------------------------------------------------- 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 | 15 | package auth 16 | 17 | import ( 18 | "sync" 19 | 20 | "github.com/prometheus/client_golang/prometheus" 21 | ) 22 | 23 | var ( 24 | currentAuthRevision = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ 25 | Namespace: "etcd_debugging", 26 | Subsystem: "auth", 27 | Name: "revision", 28 | Help: "The current revision of auth store.", 29 | }, 30 | func() float64 { 31 | reportCurrentAuthRevMu.RLock() 32 | defer reportCurrentAuthRevMu.RUnlock() 33 | return reportCurrentAuthRev() 34 | }, 35 | ) 36 | // overridden by auth store initialization 37 | reportCurrentAuthRevMu sync.RWMutex 38 | reportCurrentAuthRev = func() float64 { return 0 } 39 | ) 40 | 41 | func init() { 42 | prometheus.MustRegister(currentAuthRevision) 43 | } 44 | -------------------------------------------------------------------------------- /etcd/server/auth/nop.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package auth 16 | 17 | import ( 18 | "context" 19 | ) 20 | 21 | type tokenNop struct{} 22 | 23 | func (t *tokenNop) enable() {} 24 | func (t *tokenNop) disable() {} 25 | func (t *tokenNop) invalidateUser(string) {} 26 | func (t *tokenNop) genTokenPrefix() (string, error) { return "", nil } 27 | func (t *tokenNop) info(ctx context.Context, token string, rev uint64) (*AuthInfo, bool) { 28 | return nil, false 29 | } 30 | func (t *tokenNop) assign(ctx context.Context, username string, revision uint64) (string, error) { 31 | return "", ErrAuthFailed 32 | } 33 | func newTokenProviderNop() (*tokenNop, error) { 34 | return &tokenNop{}, nil 35 | } 36 | -------------------------------------------------------------------------------- /etcd/server/embed/auth_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2020 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 | 15 | package embed 16 | 17 | import ( 18 | "context" 19 | "testing" 20 | 21 | "go.etcd.io/etcd/server/v3/etcdserver/api/v3client" 22 | ) 23 | 24 | func TestEnableAuth(t *testing.T) { 25 | tdir := t.TempDir() 26 | cfg := NewConfig() 27 | cfg.Dir = tdir 28 | e, err := StartEtcd(cfg) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | defer e.Close() 33 | client := v3client.New(e.Server) 34 | defer client.Close() 35 | 36 | _, err = client.RoleAdd(context.TODO(), "root") 37 | if err != nil { 38 | t.Fatal(err) 39 | } 40 | _, err = client.UserAdd(context.TODO(), "root", "root") 41 | if err != nil { 42 | t.Fatal(err) 43 | } 44 | _, err = client.UserGrantRole(context.TODO(), "root", "root") 45 | if err != nil { 46 | t.Fatal(err) 47 | } 48 | _, err = client.AuthEnable(context.TODO()) 49 | if err != nil { 50 | t.Fatal(err) 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /etcd/server/embed/config_logging_journal_unix.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | //go:build !windows 16 | // +build !windows 17 | 18 | package embed 19 | 20 | import ( 21 | "fmt" 22 | "os" 23 | 24 | "go.etcd.io/etcd/client/pkg/v3/logutil" 25 | 26 | "go.uber.org/zap/zapcore" 27 | ) 28 | 29 | // use stderr as fallback 30 | func getJournalWriteSyncer() (zapcore.WriteSyncer, error) { 31 | jw, err := logutil.NewJournalWriter(os.Stderr) 32 | if err != nil { 33 | return nil, fmt.Errorf("can't find journal (%v)", err) 34 | } 35 | return zapcore.AddSync(jw), nil 36 | } 37 | -------------------------------------------------------------------------------- /etcd/server/embed/config_logging_journal_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | //go:build windows 16 | // +build windows 17 | 18 | package embed 19 | 20 | import ( 21 | "os" 22 | 23 | "go.uber.org/zap/zapcore" 24 | ) 25 | 26 | func getJournalWriteSyncer() (zapcore.WriteSyncer, error) { 27 | return zapcore.AddSync(os.Stderr), nil 28 | } 29 | -------------------------------------------------------------------------------- /etcd/server/embed/doc.go: -------------------------------------------------------------------------------- 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 | 15 | /* 16 | Package embed provides bindings for embedding an etcd server in a program. 17 | 18 | Launch an embedded etcd server using the configuration defaults: 19 | 20 | import ( 21 | "log" 22 | "time" 23 | 24 | "go.etcd.io/etcd/server/v3/embed" 25 | ) 26 | 27 | func main() { 28 | cfg := embed.NewConfig() 29 | cfg.Dir = "default.etcd" 30 | e, err := embed.StartEtcd(cfg) 31 | if err != nil { 32 | log.Fatal(err) 33 | } 34 | defer e.Close() 35 | select { 36 | case <-e.Server.ReadyNotify(): 37 | log.Printf("Server is ready!") 38 | case <-time.After(60 * time.Second): 39 | e.Server.Stop() // trigger a shutdown 40 | log.Printf("Server took too long to start!") 41 | } 42 | log.Fatal(<-e.Err()) 43 | } 44 | */ 45 | package embed 46 | -------------------------------------------------------------------------------- /etcd/server/embed/util.go: -------------------------------------------------------------------------------- 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 | 15 | package embed 16 | 17 | import ( 18 | "path/filepath" 19 | 20 | "go.etcd.io/etcd/server/v3/storage/wal" 21 | ) 22 | 23 | func isMemberInitialized(cfg *Config) bool { 24 | waldir := cfg.WalDir 25 | if waldir == "" { 26 | waldir = filepath.Join(cfg.Dir, "member", "wal") 27 | } 28 | return wal.Exist(waldir) 29 | } 30 | -------------------------------------------------------------------------------- /etcd/server/etcdmain/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package etcdmain contains the main entry point for the etcd binary. 16 | package etcdmain 17 | -------------------------------------------------------------------------------- /etcd/server/etcdmain/main.go: -------------------------------------------------------------------------------- 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 | 15 | package etcdmain 16 | 17 | import ( 18 | "fmt" 19 | "os" 20 | 21 | "github.com/coreos/go-systemd/v22/daemon" 22 | "go.uber.org/zap" 23 | ) 24 | 25 | func Main(args []string) { 26 | checkSupportArch() 27 | 28 | if len(args) > 1 { 29 | cmd := args[1] 30 | switch cmd { 31 | case "gateway", "grpc-proxy": 32 | if err := rootCmd.Execute(); err != nil { 33 | fmt.Fprint(os.Stderr, err) 34 | os.Exit(1) 35 | } 36 | return 37 | } 38 | } 39 | 40 | startEtcdOrProxyV2(args) 41 | } 42 | 43 | func notifySystemd(lg *zap.Logger) { 44 | lg.Info("notifying init daemon") 45 | _, err := daemon.SdNotify(false, daemon.SdNotifyReady) 46 | if err != nil { 47 | lg.Error("failed to notify systemd for readiness", zap.Error(err)) 48 | return 49 | } 50 | lg.Info("successfully notified init daemon") 51 | } 52 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package api manages the capabilities and features that are exposed to clients by the etcd cluster. 16 | package api 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/etcdhttp/debug.go: -------------------------------------------------------------------------------- 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 | 15 | package etcdhttp 16 | 17 | import ( 18 | "expvar" 19 | "fmt" 20 | "net/http" 21 | ) 22 | 23 | const ( 24 | varsPath = "/debug/vars" 25 | ) 26 | 27 | func HandleDebug(mux *http.ServeMux) { 28 | mux.HandleFunc(varsPath, serveVars) 29 | } 30 | 31 | func serveVars(w http.ResponseWriter, r *http.Request) { 32 | if !allowMethod(w, r, "GET") { 33 | return 34 | } 35 | 36 | w.Header().Set("Content-Type", "application/json; charset=utf-8") 37 | fmt.Fprintf(w, "{\n") 38 | first := true 39 | expvar.Do(func(kv expvar.KeyValue) { 40 | if !first { 41 | fmt.Fprintf(w, ",\n") 42 | } 43 | first = false 44 | fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value) 45 | }) 46 | fmt.Fprintf(w, "\n}\n") 47 | } 48 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/etcdhttp/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package etcdhttp implements HTTP transportation layer for etcdserver. 16 | package etcdhttp 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/etcdhttp/metrics.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package etcdhttp 16 | 17 | import ( 18 | "net/http" 19 | 20 | "github.com/prometheus/client_golang/prometheus/promhttp" 21 | ) 22 | 23 | const ( 24 | PathMetrics = "/metrics" 25 | PathProxyMetrics = "/proxy/metrics" 26 | ) 27 | 28 | // HandleMetrics registers prometheus handler on '/metrics'. 29 | func HandleMetrics(mux *http.ServeMux) { 30 | mux.Handle(PathMetrics, promhttp.Handler()) 31 | } 32 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/etcdhttp/types/errors.go: -------------------------------------------------------------------------------- 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 | 15 | package httptypes 16 | 17 | import ( 18 | "encoding/json" 19 | "fmt" 20 | "net/http" 21 | ) 22 | 23 | type HTTPError struct { 24 | Message string `json:"message"` 25 | // Code is the HTTP status code 26 | Code int `json:"-"` 27 | } 28 | 29 | func (e HTTPError) Error() string { 30 | return e.Message 31 | } 32 | 33 | func (e HTTPError) WriteTo(w http.ResponseWriter) error { 34 | w.Header().Set("Content-Type", "application/json") 35 | w.WriteHeader(e.Code) 36 | b, err := json.Marshal(e) 37 | if err != nil { 38 | panic(fmt.Sprintf("failed to marshal HTTPError: %v", err)) 39 | } 40 | if _, err := w.Write(b); err != nil { 41 | return err 42 | } 43 | return nil 44 | } 45 | 46 | func NewHTTPError(code int, m string) *HTTPError { 47 | return &HTTPError{ 48 | Message: m, 49 | Code: code, 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/membership/cluster_opts.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package membership 16 | 17 | const DefaultMaxLearners = 1 18 | 19 | type ClusterOptions struct { 20 | maxLearners int 21 | } 22 | 23 | // ClusterOption are options which can be applied to the raft cluster. 24 | type ClusterOption func(*ClusterOptions) 25 | 26 | func newClusterOpts(opts ...ClusterOption) *ClusterOptions { 27 | clOpts := &ClusterOptions{} 28 | clOpts.applyOpts(opts) 29 | return clOpts 30 | } 31 | 32 | func (co *ClusterOptions) applyOpts(opts []ClusterOption) { 33 | for _, opt := range opts { 34 | opt(co) 35 | } 36 | } 37 | 38 | // WithMaxLearners sets the maximum number of learners that can exist in the cluster membership. 39 | func WithMaxLearners(max int) ClusterOption { 40 | return func(co *ClusterOptions) { 41 | co.maxLearners = max 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/membership/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package membership describes individual etcd members and clusters of members. 16 | package membership 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/membership/errors.go: -------------------------------------------------------------------------------- 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 | 15 | package membership 16 | 17 | import ( 18 | "errors" 19 | 20 | "go.etcd.io/etcd/server/v3/etcdserver/api/v2error" 21 | ) 22 | 23 | var ( 24 | ErrIDRemoved = errors.New("membership: ID removed") 25 | ErrIDExists = errors.New("membership: ID exists") 26 | ErrIDNotFound = errors.New("membership: ID not found") 27 | ErrPeerURLexists = errors.New("membership: peerURL exists") 28 | ErrMemberNotLearner = errors.New("membership: can only promote a learner member") 29 | ErrTooManyLearners = errors.New("membership: too many learner members in cluster") 30 | ) 31 | 32 | func isKeyNotFound(err error) bool { 33 | e, ok := err.(*v2error.Error) 34 | return ok && e.ErrorCode == v2error.EcodeKeyNotFound 35 | } 36 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/membership/metrics.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package membership 16 | 17 | import "github.com/prometheus/client_golang/prometheus" 18 | 19 | var ( 20 | ClusterVersionMetrics = prometheus.NewGaugeVec(prometheus.GaugeOpts{ 21 | Namespace: "etcd", 22 | Subsystem: "cluster", 23 | Name: "version", 24 | Help: "Which version is running. 1 for 'cluster_version' label with current cluster version", 25 | }, 26 | []string{"cluster_version"}) 27 | knownPeers = prometheus.NewGaugeVec(prometheus.GaugeOpts{ 28 | Namespace: "etcd", 29 | Subsystem: "network", 30 | Name: "known_peers", 31 | Help: "The current number of known peers.", 32 | }, 33 | []string{"Local", "Remote"}, 34 | ) 35 | ) 36 | 37 | func init() { 38 | prometheus.MustRegister(ClusterVersionMetrics) 39 | prometheus.MustRegister(knownPeers) 40 | } 41 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/rafthttp/coder.go: -------------------------------------------------------------------------------- 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 | 15 | package rafthttp 16 | 17 | import "go.etcd.io/etcd/raft/v3/raftpb" 18 | 19 | type encoder interface { 20 | // encode encodes the given message to an output stream. 21 | encode(m *raftpb.Message) error 22 | } 23 | 24 | type decoder interface { 25 | // decode decodes the message from an input stream. 26 | decode() (raftpb.Message, error) 27 | } 28 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/rafthttp/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package rafthttp implements HTTP transportation layer for etcd/raft pkg. 16 | package rafthttp 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/rafthttp/fake_roundtripper_test.go: -------------------------------------------------------------------------------- 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 | 15 | package rafthttp 16 | 17 | import ( 18 | "errors" 19 | "net/http" 20 | ) 21 | 22 | func (t *roundTripperBlocker) RoundTrip(req *http.Request) (*http.Response, error) { 23 | c := make(chan struct{}, 1) 24 | t.mu.Lock() 25 | t.cancel[req] = c 26 | t.mu.Unlock() 27 | ctx := req.Context() 28 | select { 29 | case <-t.unblockc: 30 | return &http.Response{StatusCode: http.StatusNoContent, Body: &nopReadCloser{}}, nil 31 | case <-ctx.Done(): 32 | return nil, errors.New("request canceled") 33 | case <-c: 34 | return nil, errors.New("request canceled") 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/snap/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package snap handles Raft nodes' states with snapshots. 16 | // The snapshot logic is internal to etcd server and raft package. 17 | package snap 18 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/snap/snappb/snap.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | package snappb; 3 | 4 | import "gogoproto/gogo.proto"; 5 | 6 | option (gogoproto.marshaler_all) = true; 7 | option (gogoproto.sizer_all) = true; 8 | option (gogoproto.unmarshaler_all) = true; 9 | option (gogoproto.goproto_getters_all) = false; 10 | 11 | message snapshot { 12 | optional uint32 crc = 1 [(gogoproto.nullable) = false]; 13 | optional bytes data = 2; 14 | } 15 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v2store/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package v2store defines etcd's in-memory key/value store in v2 API. 16 | // To be deprecated in favor of v3 storage. 17 | package v2store 18 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v2store/event_queue.go: -------------------------------------------------------------------------------- 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 | 15 | package v2store 16 | 17 | type eventQueue struct { 18 | Events []*Event 19 | Size int 20 | Front int 21 | Back int 22 | Capacity int 23 | } 24 | 25 | func (eq *eventQueue) insert(e *Event) { 26 | eq.Events[eq.Back] = e 27 | eq.Back = (eq.Back + 1) % eq.Capacity 28 | 29 | if eq.Size == eq.Capacity { //dequeue 30 | eq.Front = (eq.Front + 1) % eq.Capacity 31 | } else { 32 | eq.Size++ 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v3compactor/compactor_test.go: -------------------------------------------------------------------------------- 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 | 15 | package v3compactor 16 | 17 | import ( 18 | "context" 19 | "sync/atomic" 20 | 21 | pb "go.etcd.io/etcd/api/v3/etcdserverpb" 22 | "go.etcd.io/etcd/client/pkg/v3/testutil" 23 | ) 24 | 25 | type fakeCompactable struct { 26 | testutil.Recorder 27 | } 28 | 29 | func (fc *fakeCompactable) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) { 30 | fc.Record(testutil.Action{Name: "c", Params: []interface{}{r}}) 31 | return &pb.CompactionResponse{}, nil 32 | } 33 | 34 | type fakeRevGetter struct { 35 | testutil.Recorder 36 | rev int64 37 | } 38 | 39 | func (fr *fakeRevGetter) Rev() int64 { 40 | fr.Record(testutil.Action{Name: "g"}) 41 | rev := atomic.AddInt64(&fr.rev, 1) 42 | return rev 43 | } 44 | 45 | func (fr *fakeRevGetter) SetRev(rev int64) { 46 | atomic.StoreInt64(&fr.rev, rev) 47 | } 48 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v3compactor/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package v3compactor implements automated policies for compacting etcd's mvcc storage. 16 | package v3compactor 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v3election/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package v3election provides a v3 election service from an etcdserver. 16 | package v3election 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v3lock/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package v3lock provides a v3 locking service from an etcdserver. 16 | package v3lock 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/api/v3rpc/codec.go: -------------------------------------------------------------------------------- 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 | 15 | package v3rpc 16 | 17 | import "github.com/golang/protobuf/proto" 18 | 19 | type codec struct{} 20 | 21 | func (c *codec) Marshal(v interface{}) ([]byte, error) { 22 | b, err := proto.Marshal(v.(proto.Message)) 23 | sentBytes.Add(float64(len(b))) 24 | return b, err 25 | } 26 | 27 | func (c *codec) Unmarshal(data []byte, v interface{}) error { 28 | receivedBytes.Add(float64(len(data))) 29 | return proto.Unmarshal(data, v.(proto.Message)) 30 | } 31 | 32 | func (c *codec) String() string { 33 | return "proto" 34 | } 35 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/apply/metrics.go: -------------------------------------------------------------------------------- 1 | // Copyright 2022 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 | 15 | package apply 16 | 17 | import "github.com/prometheus/client_golang/prometheus" 18 | 19 | var ( 20 | alarms = prometheus.NewGaugeVec(prometheus.GaugeOpts{ 21 | Namespace: "etcd_debugging", 22 | Subsystem: "server", 23 | Name: "alarms", 24 | Help: "Alarms for every member in cluster. 1 for 'server_id' label with current ID. 2 for 'alarm_type' label with type of this alarm", 25 | }, 26 | []string{"server_id", "alarm_type"}) 27 | ) 28 | 29 | func init() { 30 | prometheus.MustRegister(alarms) 31 | } 32 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/cindex/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package cindex provides an interface and implementation for getting/saving consistentIndex. 16 | package cindex 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package etcdserver defines how etcd servers interact and store their states. 16 | package etcdserver 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/version/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | // Package version provides functions for getting/saving storage version. 16 | package version 17 | -------------------------------------------------------------------------------- /etcd/server/etcdserver/version/errors.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package version 16 | 17 | import "errors" 18 | 19 | var ( 20 | ErrInvalidDowngradeTargetVersion = errors.New("etcdserver: invalid downgrade target version") 21 | ErrDowngradeInProcess = errors.New("etcdserver: cluster has a downgrade job in progress") 22 | ErrNoInflightDowngrade = errors.New("etcdserver: no inflight downgrade job") 23 | ) 24 | -------------------------------------------------------------------------------- /etcd/server/lease/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package lease provides an interface and implementation for time-limited leases over arbitrary resources. 16 | package lease 17 | -------------------------------------------------------------------------------- /etcd/server/lease/leasehttp/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package leasehttp serves lease renewals made through HTTP requests. 16 | package leasehttp 17 | -------------------------------------------------------------------------------- /etcd/server/lease/leasepb/lease.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package leasepb; 3 | 4 | import "gogoproto/gogo.proto"; 5 | import "etcd/api/etcdserverpb/rpc.proto"; 6 | 7 | option (gogoproto.marshaler_all) = true; 8 | option (gogoproto.sizer_all) = true; 9 | option (gogoproto.unmarshaler_all) = true; 10 | option (gogoproto.goproto_getters_all) = false; 11 | option (gogoproto.goproto_enum_prefix_all) = false; 12 | 13 | message Lease { 14 | int64 ID = 1; 15 | int64 TTL = 2; 16 | int64 RemainingTTL = 3; 17 | } 18 | 19 | message LeaseInternalRequest { 20 | etcdserverpb.LeaseTimeToLiveRequest LeaseTimeToLiveRequest = 1; 21 | } 22 | 23 | message LeaseInternalResponse { 24 | etcdserverpb.LeaseTimeToLiveResponse LeaseTimeToLiveResponse = 1; 25 | } 26 | -------------------------------------------------------------------------------- /etcd/server/main.go: -------------------------------------------------------------------------------- 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 | 15 | // Package main is a simple wrapper of the real etcd entrypoint package 16 | // (located at go.etcd.io/etcd/etcdmain) to ensure that etcd is still 17 | // "go getable"; e.g. `go get go.etcd.io/etcd` works as expected and 18 | // builds a binary in $GOBIN/etcd 19 | // 20 | // This package should NOT be extended or modified in any way; to modify the 21 | // etcd binary, work in the `go.etcd.io/etcd/etcdmain` package. 22 | package main 23 | 24 | import ( 25 | "os" 26 | 27 | "go.etcd.io/etcd/server/v3/etcdmain" 28 | ) 29 | 30 | func main() { 31 | etcdmain.Main(os.Args) 32 | } 33 | -------------------------------------------------------------------------------- /etcd/server/proxy/grpcproxy/adapter/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | // Package adapter provides gRPC adapters between client and server 16 | // gRPC interfaces without needing to go through a gRPC connection. 17 | package adapter 18 | -------------------------------------------------------------------------------- /etcd/server/proxy/grpcproxy/adapter/lock_client_adapter.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | package adapter 16 | 17 | import ( 18 | "context" 19 | 20 | "go.etcd.io/etcd/server/v3/etcdserver/api/v3lock/v3lockpb" 21 | 22 | "google.golang.org/grpc" 23 | ) 24 | 25 | type ls2lsc struct{ ls v3lockpb.LockServer } 26 | 27 | func LockServerToLockClient(ls v3lockpb.LockServer) v3lockpb.LockClient { 28 | return &ls2lsc{ls} 29 | } 30 | 31 | func (s *ls2lsc) Lock(ctx context.Context, r *v3lockpb.LockRequest, opts ...grpc.CallOption) (*v3lockpb.LockResponse, error) { 32 | return s.ls.Lock(ctx, r) 33 | } 34 | 35 | func (s *ls2lsc) Unlock(ctx context.Context, r *v3lockpb.UnlockRequest, opts ...grpc.CallOption) (*v3lockpb.UnlockResponse, error) { 36 | return s.ls.Unlock(ctx, r) 37 | } 38 | -------------------------------------------------------------------------------- /etcd/server/proxy/grpcproxy/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package grpcproxy is an OSI level 7 proxy for etcd v3 API requests. 16 | package grpcproxy 17 | -------------------------------------------------------------------------------- /etcd/server/proxy/grpcproxy/lock.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 The etcd Lockors 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 | 15 | package grpcproxy 16 | 17 | import ( 18 | "context" 19 | 20 | clientv3 "go.etcd.io/etcd/client/v3" 21 | "go.etcd.io/etcd/server/v3/etcdserver/api/v3lock/v3lockpb" 22 | ) 23 | 24 | type lockProxy struct { 25 | lockClient v3lockpb.LockClient 26 | } 27 | 28 | func NewLockProxy(client *clientv3.Client) v3lockpb.LockServer { 29 | return &lockProxy{lockClient: v3lockpb.NewLockClient(client.ActiveConnection())} 30 | } 31 | 32 | func (lp *lockProxy) Lock(ctx context.Context, req *v3lockpb.LockRequest) (*v3lockpb.LockResponse, error) { 33 | return lp.lockClient.Lock(ctx, req) 34 | } 35 | 36 | func (lp *lockProxy) Unlock(ctx context.Context, req *v3lockpb.UnlockRequest) (*v3lockpb.UnlockResponse, error) { 37 | return lp.lockClient.Unlock(ctx, req) 38 | } 39 | -------------------------------------------------------------------------------- /etcd/server/proxy/tcpproxy/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package tcpproxy is an OSI level 4 proxy for routing etcd clients to etcd servers. 16 | package tcpproxy 17 | -------------------------------------------------------------------------------- /etcd/server/storage/backend/config_default.go: -------------------------------------------------------------------------------- 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 | 15 | //go:build !linux && !windows 16 | // +build !linux,!windows 17 | 18 | package backend 19 | 20 | import bolt "go.etcd.io/bbolt" 21 | 22 | var boltOpenOptions *bolt.Options 23 | 24 | func (bcfg *BackendConfig) mmapSize() int { return int(bcfg.MmapSize) } 25 | -------------------------------------------------------------------------------- /etcd/server/storage/backend/config_linux.go: -------------------------------------------------------------------------------- 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 | 15 | package backend 16 | 17 | import ( 18 | "syscall" 19 | 20 | bolt "go.etcd.io/bbolt" 21 | ) 22 | 23 | // syscall.MAP_POPULATE on linux 2.6.23+ does sequential read-ahead 24 | // which can speed up entire-database read with boltdb. We want to 25 | // enable MAP_POPULATE for faster key-value store recovery in storage 26 | // package. If your kernel version is lower than 2.6.23 27 | // (https://github.com/torvalds/linux/releases/tag/v2.6.23), mmap might 28 | // silently ignore this flag. Please update your kernel to prevent this. 29 | var boltOpenOptions = &bolt.Options{ 30 | MmapFlags: syscall.MAP_POPULATE, 31 | NoFreelistSync: true, 32 | } 33 | 34 | func (bcfg *BackendConfig) mmapSize() int { return int(bcfg.MmapSize) } 35 | -------------------------------------------------------------------------------- /etcd/server/storage/backend/config_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright 2017 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 | 15 | //go:build windows 16 | // +build windows 17 | 18 | package backend 19 | 20 | import bolt "go.etcd.io/bbolt" 21 | 22 | var boltOpenOptions *bolt.Options = nil 23 | 24 | // setting mmap size != 0 on windows will allocate the entire 25 | // mmap size for the file, instead of growing it. So, force 0. 26 | 27 | func (bcfg *BackendConfig) mmapSize() int { return 0 } 28 | -------------------------------------------------------------------------------- /etcd/server/storage/backend/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package backend defines a standard interface for etcd's backend MVCC storage. 16 | package backend 17 | -------------------------------------------------------------------------------- /etcd/server/storage/backend/export_test.go: -------------------------------------------------------------------------------- 1 | package backend 2 | 3 | import bolt "go.etcd.io/bbolt" 4 | 5 | func DbFromBackendForTest(b Backend) *bolt.DB { 6 | return b.(*backend).db 7 | } 8 | 9 | func DefragLimitForTest() int { 10 | return defragLimit 11 | } 12 | 13 | func CommitsForTest(b Backend) int64 { 14 | return b.(*backend).Commits() 15 | } 16 | -------------------------------------------------------------------------------- /etcd/server/storage/backend/hooks.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package backend 16 | 17 | type HookFunc func(tx BatchTx) 18 | 19 | // Hooks allow to add additional logic executed during transaction lifetime. 20 | type Hooks interface { 21 | // OnPreCommitUnsafe is executed before Commit of transactions. 22 | // The given transaction is already locked. 23 | OnPreCommitUnsafe(tx BatchTx) 24 | } 25 | 26 | type hooks struct { 27 | onPreCommitUnsafe HookFunc 28 | } 29 | 30 | func (h hooks) OnPreCommitUnsafe(tx BatchTx) { 31 | h.onPreCommitUnsafe(tx) 32 | } 33 | 34 | func NewHooks(onPreCommitUnsafe HookFunc) Hooks { 35 | return hooks{onPreCommitUnsafe: onPreCommitUnsafe} 36 | } 37 | -------------------------------------------------------------------------------- /etcd/server/storage/datadir/datadir.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package datadir 16 | 17 | import "path/filepath" 18 | 19 | const ( 20 | memberDirSegment = "member" 21 | snapDirSegment = "snap" 22 | walDirSegment = "wal" 23 | backendFileSegment = "db" 24 | ) 25 | 26 | func ToBackendFileName(dataDir string) string { 27 | return filepath.Join(ToSnapDir(dataDir), backendFileSegment) 28 | } 29 | 30 | func ToSnapDir(dataDir string) string { 31 | return filepath.Join(ToMemberDir(dataDir), snapDirSegment) 32 | } 33 | 34 | func ToWalDir(dataDir string) string { 35 | return filepath.Join(ToMemberDir(dataDir), walDirSegment) 36 | } 37 | 38 | func ToMemberDir(dataDir string) string { 39 | return filepath.Join(dataDir, memberDirSegment) 40 | } 41 | -------------------------------------------------------------------------------- /etcd/server/storage/datadir/datadir_test.go: -------------------------------------------------------------------------------- 1 | package datadir_test 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | "go.etcd.io/etcd/server/v3/storage/datadir" 8 | ) 9 | 10 | func TestToBackendFileName(t *testing.T) { 11 | result := datadir.ToBackendFileName("/dir/data-dir") 12 | assert.Equal(t, "/dir/data-dir/member/snap/db", result) 13 | } 14 | 15 | func TestToMemberDir(t *testing.T) { 16 | result := datadir.ToMemberDir("/dir/data-dir") 17 | assert.Equal(t, "/dir/data-dir/member", result) 18 | } 19 | 20 | func TestToSnapDir(t *testing.T) { 21 | result := datadir.ToSnapDir("/dir/data-dir") 22 | assert.Equal(t, "/dir/data-dir/member/snap", result) 23 | } 24 | 25 | func TestToWalDir(t *testing.T) { 26 | result := datadir.ToWalDir("/dir/data-dir") 27 | assert.Equal(t, "/dir/data-dir/member/wal", result) 28 | } 29 | 30 | func TestToWalDirSlash(t *testing.T) { 31 | result := datadir.ToWalDir("/dir/data-dir/") 32 | assert.Equal(t, "/dir/data-dir/member/wal", result) 33 | } 34 | -------------------------------------------------------------------------------- /etcd/server/storage/datadir/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package datadir 16 | 17 | // datadir contains functions to navigate file-layout of etcd data-directory. 18 | -------------------------------------------------------------------------------- /etcd/server/storage/metrics.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package storage 16 | 17 | import ( 18 | "github.com/prometheus/client_golang/prometheus" 19 | ) 20 | 21 | var quotaBackendBytes = prometheus.NewGauge(prometheus.GaugeOpts{ 22 | Namespace: "etcd", 23 | Subsystem: "server", 24 | Name: "quota_backend_bytes", 25 | Help: "Current backend storage quota size in bytes.", 26 | }) 27 | 28 | func init() { 29 | prometheus.MustRegister(quotaBackendBytes) 30 | } 31 | -------------------------------------------------------------------------------- /etcd/server/storage/mvcc/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // Package mvcc defines etcd's stable MVCC storage. 16 | package mvcc 17 | -------------------------------------------------------------------------------- /etcd/server/storage/mvcc/watcher_bench_test.go: -------------------------------------------------------------------------------- 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 | 15 | package mvcc 16 | 17 | import ( 18 | "fmt" 19 | "testing" 20 | 21 | "go.etcd.io/etcd/server/v3/lease" 22 | betesting "go.etcd.io/etcd/server/v3/storage/backend/testing" 23 | "go.uber.org/zap/zaptest" 24 | ) 25 | 26 | func BenchmarkKVWatcherMemoryUsage(b *testing.B) { 27 | be, tmpPath := betesting.NewDefaultTmpBackend(b) 28 | watchable := newWatchableStore(zaptest.NewLogger(b), be, &lease.FakeLessor{}, StoreConfig{}) 29 | 30 | defer cleanup(watchable, be, tmpPath) 31 | 32 | w := watchable.NewWatchStream() 33 | 34 | b.ReportAllocs() 35 | b.StartTimer() 36 | for i := 0; i < b.N; i++ { 37 | w.Watch(0, []byte(fmt.Sprint("foo", i)), nil, 0) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /etcd/server/storage/wal/file_pipeline_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 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 | 15 | package wal 16 | 17 | import ( 18 | "math" 19 | "testing" 20 | 21 | "go.uber.org/zap/zaptest" 22 | ) 23 | 24 | func TestFilePipeline(t *testing.T) { 25 | tdir := t.TempDir() 26 | 27 | fp := newFilePipeline(zaptest.NewLogger(t), tdir, SegmentSizeBytes) 28 | defer fp.Close() 29 | 30 | f, ferr := fp.Open() 31 | if ferr != nil { 32 | t.Fatal(ferr) 33 | } 34 | f.Close() 35 | } 36 | 37 | func TestFilePipelineFailPreallocate(t *testing.T) { 38 | tdir := t.TempDir() 39 | 40 | fp := newFilePipeline(zaptest.NewLogger(t), tdir, math.MaxInt64) 41 | defer fp.Close() 42 | 43 | f, ferr := fp.Open() 44 | if f != nil || ferr == nil { // no space left on device 45 | t.Fatal("expected error on invalid pre-allocate size, but no error") 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /etcd/server/storage/wal/walpb/record.go: -------------------------------------------------------------------------------- 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 | 15 | package walpb 16 | 17 | import "errors" 18 | 19 | var ( 20 | ErrCRCMismatch = errors.New("walpb: crc mismatch") 21 | ) 22 | 23 | func (rec *Record) Validate(crc uint32) error { 24 | if rec.Crc == crc { 25 | return nil 26 | } 27 | rec.Reset() 28 | return ErrCRCMismatch 29 | } 30 | 31 | // ValidateSnapshotForWrite ensures the Snapshot the newly written snapshot is valid. 32 | // 33 | // There might exist log-entries written by old etcd versions that does not conform 34 | // to the requirements. 35 | func ValidateSnapshotForWrite(e *Snapshot) error { 36 | // Since etcd>=3.5.0 37 | if e.ConfState == nil && e.Index > 0 { 38 | return errors.New("Saved (not-initial) snapshot is missing ConfState: " + e.String()) 39 | } 40 | return nil 41 | } 42 | -------------------------------------------------------------------------------- /etcd/server/storage/wal/walpb/record.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | package walpb; 3 | 4 | import "gogoproto/gogo.proto"; 5 | import "raft/raftpb/raft.proto"; 6 | 7 | option (gogoproto.marshaler_all) = true; 8 | option (gogoproto.sizer_all) = true; 9 | option (gogoproto.unmarshaler_all) = true; 10 | option (gogoproto.goproto_getters_all) = false; 11 | 12 | message Record { 13 | optional int64 type = 1 [(gogoproto.nullable) = false]; 14 | optional uint32 crc = 2 [(gogoproto.nullable) = false]; 15 | optional bytes data = 3; 16 | } 17 | 18 | // Keep in sync with raftpb.SnapshotMetadata. 19 | message Snapshot { 20 | optional uint64 index = 1 [(gogoproto.nullable) = false]; 21 | optional uint64 term = 2 [(gogoproto.nullable) = false]; 22 | // Field populated since >=etcd-3.5.0. 23 | optional raftpb.ConfState conf_state = 3; 24 | } 25 | -------------------------------------------------------------------------------- /etcd/server/storage/wal/walpb/record_test.go: -------------------------------------------------------------------------------- 1 | package walpb 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/golang/protobuf/descriptor" 7 | "go.etcd.io/etcd/raft/v3/raftpb" 8 | ) 9 | 10 | func TestSnapshotMetadataCompatibility(t *testing.T) { 11 | _, snapshotMetadataMd := descriptor.ForMessage(&raftpb.SnapshotMetadata{}) 12 | _, snapshotMd := descriptor.ForMessage(&Snapshot{}) 13 | if len(snapshotMetadataMd.GetField()) != len(snapshotMd.GetField()) { 14 | t.Errorf("Different number of fields in raftpb.SnapshotMetadata vs. walpb.Snapshot. " + 15 | "They are supposed to be in sync.") 16 | } 17 | } 18 | 19 | func TestValidateSnapshot(t *testing.T) { 20 | tests := []struct { 21 | name string 22 | snap *Snapshot 23 | wantErr bool 24 | }{ 25 | {name: "empty", snap: &Snapshot{}, wantErr: false}, 26 | {name: "invalid", snap: &Snapshot{Index: 5, Term: 3}, wantErr: true}, 27 | {name: "valid", snap: &Snapshot{Index: 5, Term: 3, ConfState: &raftpb.ConfState{Voters: []uint64{0x00cad1}}}, wantErr: false}, 28 | } 29 | for _, tt := range tests { 30 | t.Run(tt.name, func(t *testing.T) { 31 | if err := ValidateSnapshotForWrite(tt.snap); (err != nil) != tt.wantErr { 32 | t.Errorf("ValidateSnapshotForWrite() error = %v, wantErr %v", err, tt.wantErr) 33 | } 34 | }) 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /etcd/server/verify/doc.go: -------------------------------------------------------------------------------- 1 | // Copyright 2021 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 | 15 | package verify 16 | 17 | // verify package is analyzing persistent state of etcd to find potential 18 | // inconsistencies. 19 | // In particular it covers cross-checking between different aspacts of etcd 20 | // storage like WAL & Backend. 21 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/innovationb1ue/RedisGO/config" 7 | "github.com/innovationb1ue/RedisGO/logger" 8 | "github.com/innovationb1ue/RedisGO/memdb" 9 | "github.com/innovationb1ue/RedisGO/server" 10 | ) 11 | 12 | func init() { 13 | // Register commands 14 | memdb.RegisterKeyCommands() 15 | memdb.RegisterStringCommands() 16 | memdb.RegisterListCommands() 17 | memdb.RegisterSetCommands() 18 | memdb.RegisterHashCommands() 19 | memdb.RegisterPubSubCommands() 20 | memdb.RegisterSortedSetCommands() 21 | memdb.RegisterStreamCommands() 22 | memdb.RegisterRaftCommand() 23 | } 24 | 25 | func main() { 26 | // setup config 27 | cfg, err := config.Setup() 28 | if err != nil { 29 | logger.Error(err) 30 | os.Exit(1) 31 | } 32 | // setup logger 33 | err = logger.SetUp(cfg) 34 | if err != nil { 35 | logger.Error(err) 36 | os.Exit(1) 37 | } 38 | // setup tcp server 39 | err = server.Start(cfg) 40 | if err != nil { 41 | logger.Error(err) 42 | os.Exit(1) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /memdb/command.go: -------------------------------------------------------------------------------- 1 | package memdb 2 | 3 | import ( 4 | "context" 5 | "github.com/innovationb1ue/RedisGO/resp" 6 | "net" 7 | "strings" 8 | ) 9 | 10 | type cmdBytes = [][]byte 11 | 12 | // CmdTable holds all registered commands 13 | var CmdTable = make(map[string]*command) 14 | 15 | // We allow executor to directly write message back to the tcp connection for some blocking commands. 16 | // But it should never be spoilt. Normal commands should always return a data but not write 17 | // into the pipe by themselves. 18 | type cmdExecutor func(ctx context.Context, m *MemDb, cmd [][]byte, conn net.Conn) resp.RedisData 19 | 20 | type command struct { 21 | Executor cmdExecutor 22 | } 23 | 24 | func RegisterCommand(cmdName string, executor cmdExecutor) { 25 | CmdTable[cmdName] = &command{ 26 | Executor: executor, 27 | } 28 | } 29 | 30 | func MakeCommandBytes(input string) cmdBytes { 31 | cmdStrs := strings.Split(input, " ") 32 | cmds := make(cmdBytes, 0) 33 | for _, c := range cmdStrs { 34 | cmds = append(cmds, []byte(c)) 35 | } 36 | return cmds 37 | } 38 | -------------------------------------------------------------------------------- /memdb/sorted_set_struct.go: -------------------------------------------------------------------------------- 1 | package memdb 2 | 3 | import "github.com/innovationb1ue/RedisGO/logger" 4 | 5 | type SortedSet[T Val] struct { 6 | *Btree[T] 7 | } 8 | 9 | func NewSortedSet() *SortedSet[*SortedSetNode] { 10 | return &SortedSet[*SortedSetNode]{NewBtree[*SortedSetNode]()} 11 | } 12 | 13 | type SortedSetNode struct { 14 | Names map[string]struct{} // allow multiple member with the same value 15 | Score float64 16 | } 17 | 18 | func (n *SortedSetNode) Empty() { 19 | n.Names = nil 20 | } 21 | 22 | func (n *SortedSetNode) SetScore(score float64) { 23 | n.Score = score 24 | } 25 | 26 | func (n *SortedSetNode) GetScore() float64 { 27 | return n.Score 28 | } 29 | 30 | func (n *SortedSetNode) IsNameExist(name string) bool { 31 | _, ok := n.Names[name] 32 | return ok 33 | } 34 | 35 | func (n *SortedSetNode) AddName(name string) { 36 | n.Names[name] = struct{}{} 37 | } 38 | 39 | func (n *SortedSetNode) DeleteName(name string) { 40 | delete(n.Names, name) 41 | } 42 | 43 | func (n *SortedSetNode) Comp(val Val) int64 { 44 | n2 := val.(*SortedSetNode) 45 | if n.Score > n2.Score { 46 | return 1 47 | } else if n.Score < n2.Score { 48 | return -1 49 | } else if n.Score == n2.Score { 50 | return 0 51 | } else { 52 | logger.Error("cant compare values in sorted set Comp") 53 | return 0 54 | } 55 | } 56 | 57 | func (n *SortedSetNode) GetNames() map[string]struct{} { 58 | return n.Names 59 | } 60 | -------------------------------------------------------------------------------- /raftexample/doc.go: -------------------------------------------------------------------------------- 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 | 15 | // raftexample is a simple KV store using the raft and rafthttp libraries. 16 | package raftexample 17 | -------------------------------------------------------------------------------- /raftexample/kvstore_test.go: -------------------------------------------------------------------------------- 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 | 15 | package raftexample 16 | 17 | import ( 18 | "reflect" 19 | "testing" 20 | ) 21 | 22 | func Test_kvstore_snapshot(t *testing.T) { 23 | tm := map[string]string{"foo": "bar"} 24 | s := &kvstore{kvStore: tm} 25 | 26 | v, _ := s.Lookup("foo") 27 | if v != "bar" { 28 | t.Fatalf("foo has unexpected value, got %s", v) 29 | } 30 | 31 | data, err := s.getSnapshot() 32 | if err != nil { 33 | t.Fatal(err) 34 | } 35 | s.kvStore = nil 36 | 37 | if err := s.recoverFromSnapshot(data); err != nil { 38 | t.Fatal(err) 39 | } 40 | v, _ = s.Lookup("foo") 41 | if v != "bar" { 42 | t.Fatalf("foo has unexpected value, got %s", v) 43 | } 44 | if !reflect.DeepEqual(s.kvStore, tm) { 45 | t.Fatalf("store expected %+v, got %+v", tm, s.kvStore) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /redis.conf: -------------------------------------------------------------------------------- 1 | host 127.0.0.1 2 | 3 | port 6380 4 | 5 | logdir /tmp 6 | 7 | loglevel info 8 | 9 | shardnum 1024 10 | 11 | appendonly yes 12 | 13 | databases 16 -------------------------------------------------------------------------------- /redis2.conf: -------------------------------------------------------------------------------- 1 | host 127.0.0.1 2 | 3 | port 6383 4 | 5 | logdir /tmp 6 | 7 | loglevel info 8 | 9 | shardnum 1024 10 | 11 | appendonly yes 12 | 13 | databases 16 -------------------------------------------------------------------------------- /resp/errors_singleton.go: -------------------------------------------------------------------------------- 1 | package resp 2 | 3 | // premake errors structures here to avoid frequently memory allocate 4 | -------------------------------------------------------------------------------- /util/chan_pump.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | type Pump struct { 4 | Out chan any 5 | In map[int]<-chan any 6 | index int 7 | } 8 | 9 | func NewPump() *Pump { 10 | return &Pump{ 11 | Out: make(chan any, 1), 12 | In: make(map[int]<-chan any, 0), 13 | index: -1, 14 | } 15 | } 16 | 17 | func (p *Pump) AddIn(in <-chan any) { 18 | p.index++ 19 | p.In[p.index] = in 20 | } 21 | 22 | // RunForward blocks and forwards all inbound channels to a single outbound channel for exactly 1 message. 23 | func (p *Pump) RunForward() { 24 | // chan to stop other listening goroutines 25 | msgSent := make(chan struct{}) 26 | for idx, in := range p.In { 27 | go func(in <-chan any, idx int) { 28 | select { 29 | case <-msgSent: 30 | return 31 | case <-in: 32 | // output the index of the available list 33 | close(msgSent) 34 | p.Out <- idx 35 | return 36 | } 37 | }(in, idx) 38 | } 39 | } 40 | --------------------------------------------------------------------------------