zyxxoo commented on code in PR #2301:
URL: 
https://github.com/apache/incubator-hugegraph/pull/2301#discussion_r1313840490


##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/IdCounter.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.backend.tx;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.hugegraph.backend.BackendException;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.id.IdGenerator;
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.client.PDConfig;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.Pdpb;
+import org.apache.hugegraph.store.term.HgPair;
+import org.apache.hugegraph.type.HugeType;
+import org.apache.hugegraph.util.E;
+
+public class IdCounter {
+
+    private static final int TIMES = 10000;
+    private static final int DELTA = 10000;
+    private static final String DELIMITER = "/";
+    private static Map<String, HgPair> ids = new ConcurrentHashMap<>();

Review Comment:
   volatile?



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/PdDistributedLock.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta.lock;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.pd.client.KvClient;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.kv.LockResponse;
+
+/**
+ * @author zhangyingjie
+ * @date 2022/6/18
+ **/
+public class PdDistributedLock extends AbstractDistributedLock {
+
+    private static int poolSize = 8;
+    private final KvClient client;
+    private ScheduledExecutorService service = new 
ScheduledThreadPoolExecutor(poolSize, r -> {
+        Thread t = new Thread(r);
+        t.setDaemon(true);
+        return t;
+    });
+
+    public PdDistributedLock(KvClient client) {
+        this.client = client;
+    }
+
+    @Override
+    public LockResult lock(String key, long second) {
+        long ttl = second * 1000L;
+        try {
+            LockResponse response = this.client.lock(key, ttl);
+            boolean succeed = response.getSucceed();
+            LockResult result = new LockResult();
+            if (succeed) {
+                result.setLeaseId(response.getClientId());
+                result.lockSuccess(true);
+                long period = ttl - ttl / 4;
+                ScheduledFuture<?> future = service.scheduleAtFixedRate(() -> {
+                    synchronized (result) {
+                        keepAlive(key);
+                    }
+                }, 10, period, TimeUnit.MILLISECONDS);
+                result.setFuture(future);
+            }
+            return result;
+        } catch (PDException e) {
+            throw new HugeException("Failed to lock '%s' to pd", e, key);
+        }
+    }
+
+    @Override
+    public void unLock(String key, LockResult lockResult) {
+        try {
+            LockResponse response = this.client.unlock(key);
+            boolean succeed = response.getSucceed();
+            if (succeed == false) {
+                throw new HugeException("Failed to unlock '%s' to pd", key);
+            }
+            if (lockResult.getFuture() != null) {
+                synchronized (lockResult) {

Review Comment:
   同



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/AbstractDistributedLock.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta.lock;
+
+import org.apache.hugegraph.pd.client.KvClient;
+
+import io.etcd.jetcd.Client;
+
+/**
+ * @author zhangyingjie
+ * @date 2022/6/18
+ **/
+public abstract class AbstractDistributedLock {
+
+    private static volatile AbstractDistributedLock defaultLock;
+
+    public static AbstractDistributedLock getInstance(AutoCloseable client) {

Review Comment:
   这单例不合适吧?每次都要传client



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/PdDistributedLock.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta.lock;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.pd.client.KvClient;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.kv.LockResponse;
+
+/**
+ * @author zhangyingjie
+ * @date 2022/6/18
+ **/
+public class PdDistributedLock extends AbstractDistributedLock {
+
+    private static int poolSize = 8;
+    private final KvClient client;
+    private ScheduledExecutorService service = new 
ScheduledThreadPoolExecutor(poolSize, r -> {
+        Thread t = new Thread(r);
+        t.setDaemon(true);
+        return t;
+    });
+
+    public PdDistributedLock(KvClient client) {
+        this.client = client;
+    }
+
+    @Override
+    public LockResult lock(String key, long second) {
+        long ttl = second * 1000L;
+        try {
+            LockResponse response = this.client.lock(key, ttl);
+            boolean succeed = response.getSucceed();
+            LockResult result = new LockResult();
+            if (succeed) {
+                result.setLeaseId(response.getClientId());
+                result.lockSuccess(true);
+                long period = ttl - ttl / 4;
+                ScheduledFuture<?> future = service.scheduleAtFixedRate(() -> {
+                    synchronized (result) {
+                        keepAlive(key);
+                    }
+                }, 10, period, TimeUnit.MILLISECONDS);
+                result.setFuture(future);
+            }
+            return result;
+        } catch (PDException e) {
+            throw new HugeException("Failed to lock '%s' to pd", e, key);
+        }
+    }
+
+    @Override
+    public void unLock(String key, LockResult lockResult) {
+        try {
+            LockResponse response = this.client.unlock(key);
+            boolean succeed = response.getSucceed();
+            if (succeed == false) {
+                throw new HugeException("Failed to unlock '%s' to pd", key);
+            }
+            if (lockResult.getFuture() != null) {
+                synchronized (lockResult) {

Review Comment:
   同



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/PdDistributedLock.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta.lock;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.pd.client.KvClient;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.kv.LockResponse;
+
+/**
+ * @author zhangyingjie
+ * @date 2022/6/18
+ **/
+public class PdDistributedLock extends AbstractDistributedLock {
+
+    private static int poolSize = 8;
+    private final KvClient client;
+    private ScheduledExecutorService service = new 
ScheduledThreadPoolExecutor(poolSize, r -> {
+        Thread t = new Thread(r);
+        t.setDaemon(true);
+        return t;
+    });
+
+    public PdDistributedLock(KvClient client) {
+        this.client = client;
+    }
+
+    @Override
+    public LockResult lock(String key, long second) {
+        long ttl = second * 1000L;
+        try {
+            LockResponse response = this.client.lock(key, ttl);
+            boolean succeed = response.getSucceed();
+            LockResult result = new LockResult();
+            if (succeed) {
+                result.setLeaseId(response.getClientId());
+                result.lockSuccess(true);
+                long period = ttl - ttl / 4;
+                ScheduledFuture<?> future = service.scheduleAtFixedRate(() -> {
+                    synchronized (result) {

Review Comment:
   这里为啥加锁



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/PdMetaDriver.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.meta.lock.LockResult;
+import org.apache.hugegraph.meta.lock.PdDistributedLock;
+import org.apache.hugegraph.pd.client.KvClient;
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.client.PDConfig;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.kv.KResponse;
+import org.apache.hugegraph.pd.grpc.kv.LockResponse;
+import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse;
+import org.apache.hugegraph.pd.grpc.kv.TTLResponse;
+import org.apache.hugegraph.pd.grpc.kv.WatchEvent;
+import org.apache.hugegraph.pd.grpc.kv.WatchResponse;
+import org.apache.hugegraph.pd.grpc.kv.WatchType;
+
+import com.google.common.base.Strings;
+
+public class PdMetaDriver implements MetaDriver {
+
+    KvClient<WatchResponse> client = null;

Review Comment:
   Private final



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/space/SchemaTemplate.java:
##########
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.space;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+
+import org.apache.hugegraph.util.E;
+
+import com.google.common.collect.ImmutableMap;
+
+public class SchemaTemplate {
+
+    public static SimpleDateFormat FORMATTER = new 
SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    protected Date createTime;
+    protected Date updateTime;
+    protected String creator;
+    private String name;

Review Comment:
   final



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/PdMetaDriver.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.meta.lock.LockResult;
+import org.apache.hugegraph.meta.lock.PdDistributedLock;
+import org.apache.hugegraph.pd.client.KvClient;
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.client.PDConfig;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.kv.KResponse;
+import org.apache.hugegraph.pd.grpc.kv.LockResponse;
+import org.apache.hugegraph.pd.grpc.kv.ScanPrefixResponse;
+import org.apache.hugegraph.pd.grpc.kv.TTLResponse;
+import org.apache.hugegraph.pd.grpc.kv.WatchEvent;
+import org.apache.hugegraph.pd.grpc.kv.WatchResponse;
+import org.apache.hugegraph.pd.grpc.kv.WatchType;
+
+import com.google.common.base.Strings;
+
+public class PdMetaDriver implements MetaDriver {
+
+    KvClient<WatchResponse> client = null;
+    PDClient pdClient = null;
+    private PdDistributedLock lock;
+
+    public PdMetaDriver(String pdPeer) {
+        PDConfig pdConfig = PDConfig.of(pdPeer);
+        this.client = new KvClient<>(pdConfig);
+        this.pdClient = PDClient.create(pdConfig);
+        lock = new PdDistributedLock(this.client);

Review Comment:
   This



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/AbstractDistributedLock.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta.lock;
+
+import org.apache.hugegraph.pd.client.KvClient;
+
+import io.etcd.jetcd.Client;
+
+/**
+ * @author zhangyingjie
+ * @date 2022/6/18
+ **/
+public abstract class AbstractDistributedLock {
+
+    private static volatile AbstractDistributedLock defaultLock;
+
+    public static AbstractDistributedLock getInstance(AutoCloseable client) {

Review Comment:
   这单例不合适吧?每次都要传client



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/IdCounter.java:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.backend.tx;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.hugegraph.backend.BackendException;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.id.IdGenerator;
+import org.apache.hugegraph.pd.client.PDClient;
+import org.apache.hugegraph.pd.client.PDConfig;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.Pdpb;
+import org.apache.hugegraph.store.term.HgPair;
+import org.apache.hugegraph.type.HugeType;
+import org.apache.hugegraph.util.E;
+
+public class IdCounter {
+
+    private static final int TIMES = 10000;
+    private static final int DELTA = 10000;
+    private static final String DELIMITER = "/";
+    private static Map<String, HgPair> ids = new ConcurrentHashMap<>();

Review Comment:
   volatile?



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/EtcdMetaDriver.java:
##########
@@ -0,0 +1,331 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta;
+
+import java.io.File;
+import java.net.URI;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.function.Consumer;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.meta.lock.DistributedLock;
+import org.apache.hugegraph.meta.lock.LockResult;
+import org.apache.hugegraph.type.define.CollectionType;
+import org.apache.hugegraph.util.E;
+import org.apache.hugegraph.util.collection.CollectionFactory;
+
+import com.google.common.base.Strings;
+
+import io.etcd.jetcd.ByteSequence;
+import io.etcd.jetcd.Client;
+import io.etcd.jetcd.ClientBuilder;
+import io.etcd.jetcd.KV;
+import io.etcd.jetcd.KeyValue;
+import io.etcd.jetcd.kv.GetResponse;
+import io.etcd.jetcd.lease.LeaseKeepAliveResponse;
+import io.etcd.jetcd.options.DeleteOption;
+import io.etcd.jetcd.options.GetOption;
+import io.etcd.jetcd.options.WatchOption;
+import io.etcd.jetcd.watch.WatchEvent;
+import io.etcd.jetcd.watch.WatchResponse;
+import io.netty.handler.ssl.ApplicationProtocolConfig;
+import io.netty.handler.ssl.ApplicationProtocolNames;
+import io.netty.handler.ssl.SslContext;
+import io.netty.handler.ssl.SslContextBuilder;
+import io.netty.handler.ssl.SslProvider;
+
+public class EtcdMetaDriver implements MetaDriver {
+
+    private Client client;
+    private DistributedLock lock;
+
+    public EtcdMetaDriver(String trustFile, String clientCertFile,
+                          String clientKeyFile, Object... endpoints) {
+        ClientBuilder builder = this.etcdMetaDriverBuilder(endpoints);
+
+        SslContext sslContext = openSslContext(trustFile, clientCertFile,
+                                               clientKeyFile);
+        this.client = builder.sslContext(sslContext).build();
+        this.lock = DistributedLock.getInstance(this.client);
+    }
+
+    public EtcdMetaDriver(Object... endpoints) {
+        ClientBuilder builder = this.etcdMetaDriverBuilder(endpoints);
+        this.client = builder.build();
+        this.lock = DistributedLock.getInstance(this.client);
+    }
+
+    private static ByteSequence toByteSequence(String content) {
+        return ByteSequence.from(content.getBytes());
+    }
+
+    private static boolean isEtcdPut(WatchEvent event) {
+        return event.getEventType() == WatchEvent.EventType.PUT;
+    }
+
+    public static SslContext openSslContext(String trustFile,
+                                            String clientCertFile,
+                                            String clientKeyFile) {
+        SslContext ssl;
+        try {
+            File trustManagerFile = FileUtils.getFile(trustFile);
+            File keyCertChainFile = FileUtils.getFile(clientCertFile);
+            File KeyFile = FileUtils.getFile(clientKeyFile);
+            ApplicationProtocolConfig alpn = new ApplicationProtocolConfig(
+                    ApplicationProtocolConfig.Protocol.ALPN,
+                    
ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE,
+
+                    ApplicationProtocolConfig.SelectedListenerFailureBehavior
+                            .ACCEPT,
+                    ApplicationProtocolNames.HTTP_2);
+
+            ssl = SslContextBuilder.forClient()
+                                   .applicationProtocolConfig(alpn)
+                                   .sslProvider(SslProvider.OPENSSL)
+                                   .trustManager(trustManagerFile)
+                                   .keyManager(keyCertChainFile, KeyFile)
+                                   .build();
+        } catch (Exception e) {
+            throw new HugeException("Failed to open ssl context", e);
+        }
+        return ssl;
+    }
+
+    public ClientBuilder etcdMetaDriverBuilder(Object... endpoints) {
+        int length = endpoints.length;
+        ClientBuilder builder = null;
+        if (endpoints[0] instanceof List && endpoints.length == 1) {
+            builder = Client.builder()
+                            .endpoints(((List<String>) endpoints[0])
+                                               .toArray(new String[0]));
+        } else if (endpoints[0] instanceof String) {
+            for (int i = 1; i < length; i++) {
+                E.checkArgument(endpoints[i] instanceof String,
+                                "Inconsistent endpoint %s(%s) with %s(%s)",
+                                endpoints[i], endpoints[i].getClass(),
+                                endpoints[0], endpoints[0].getClass());
+            }
+            builder = Client.builder().endpoints((String[]) endpoints);
+        } else if (endpoints[0] instanceof URI) {
+            for (int i = 1; i < length; i++) {
+                E.checkArgument(endpoints[i] instanceof String,
+                                "Invalid endpoint %s(%s)",
+                                endpoints[i], endpoints[i].getClass(),
+                                endpoints[0], endpoints[0].getClass());
+            }
+            builder = Client.builder().endpoints((URI[]) endpoints);
+        } else {
+            E.checkArgument(false, "Invalid endpoint %s(%s)",
+                            endpoints[0], endpoints[0].getClass());
+        }
+        return builder;
+    }
+
+    @Override
+    public long keepAlive(String key, long leaseId) {
+        try {
+            LeaseKeepAliveResponse response =
+                    this.client.getLeaseClient().keepAliveOnce(leaseId).get();
+            return response.getID();
+        } catch (InterruptedException | ExecutionException e) {
+            // keepAlive once Failed
+            return 0;
+        }
+    }
+
+    @Override
+    public String get(String key) {
+        List<KeyValue> keyValues;
+        KV kvClient = this.client.getKVClient();
+        try {
+            keyValues = kvClient.get(toByteSequence(key))
+                                .get().getKvs();
+        } catch (InterruptedException | ExecutionException e) {
+            throw new HugeException("Failed to get key '%s' from etcd", e, 
key);
+        }
+
+        if (keyValues.size() > 0) {
+            return 
keyValues.get(0).getValue().toString(Charset.defaultCharset());
+        }
+
+        return null;
+    }
+
+    @Override
+    public void put(String key, String value) {
+        KV kvClient = this.client.getKVClient();
+        try {
+            kvClient.put(toByteSequence(key), toByteSequence(value)).get();
+        } catch (InterruptedException | ExecutionException e) {
+            try {
+                kvClient.delete(toByteSequence(key)).get();
+            } catch (Throwable t) {
+                throw new HugeException("Failed to put '%s:%s' to etcd",
+                                        e, key, value);
+            }
+        }
+    }
+
+    @Override
+    public void delete(String key) {
+        KV kvClient = this.client.getKVClient();
+        try {
+            kvClient.delete(toByteSequence(key)).get();
+        } catch (InterruptedException | ExecutionException e) {
+            throw new HugeException(
+                    "Failed to delete key '%s' from etcd", e, key);
+        }
+    }
+
+    @Override
+    public void deleteWithPrefix(String prefix) {
+        KV kvClient = this.client.getKVClient();
+        try {
+            DeleteOption option = DeleteOption.newBuilder()
+                                              .isPrefix(true)
+                                              .build();
+            kvClient.delete(toByteSequence(prefix), option);
+        } catch (Throwable e) {
+            throw new HugeException(
+                    "Failed to delete prefix '%s' from etcd", e, prefix);
+        }
+    }
+
+    @Override
+    public Map<String, String> scanWithPrefix(String prefix) {
+        GetOption getOption = GetOption.newBuilder()
+                                       .isPrefix(true)
+                                       .build();
+        GetResponse response;
+        try {
+            response = this.client.getKVClient().get(toByteSequence(prefix),
+                                                     getOption).get();
+        } catch (InterruptedException | ExecutionException e) {
+            throw new HugeException("Failed to scan etcd with prefix '%s'",
+                                    e, prefix);
+        }
+        int size = (int) response.getCount();
+        Map<String, String> keyValues = CollectionFactory.newMap(
+                CollectionType.JCF, size);
+        for (KeyValue kv : response.getKvs()) {
+            String key = kv.getKey().toString(Charset.defaultCharset());
+            String value = kv.getValue().size() == 0 ? "" :
+                           kv.getValue().toString(Charset.defaultCharset());
+            keyValues.put(key, value);
+        }
+        return keyValues;
+    }
+
+    @Override
+    public <T> List<String> extractValuesFromResponse(T response) {
+        List<String> values = new ArrayList<>();
+        E.checkArgument(response instanceof WatchResponse,
+                        "Invalid response type %s", response.getClass());
+        for (WatchEvent event : ((WatchResponse) response).getEvents()) {
+            // Skip if not etcd PUT event
+            if (!isEtcdPut(event)) {
+                return null;
+            }
+
+            String value = event.getKeyValue().getValue()
+                                .toString(Charset.defaultCharset());
+            values.add(value);
+        }
+        return values;
+    }
+
+    @Override
+    public <T> Map<String, String> extractKVFromResponse(T response) {
+        E.checkArgument(response instanceof WatchResponse,
+                        "Invalid response type %s", response.getClass());
+
+        Map<String, String> resultMap = new HashMap<>();
+        for (WatchEvent event : ((WatchResponse) response).getEvents()) {
+            // Skip if not etcd PUT event
+            if (!isEtcdPut(event)) {
+                continue;
+            }
+
+            String key = 
event.getKeyValue().getKey().toString(Charset.defaultCharset());
+            String value = event.getKeyValue().getValue()
+                                .toString(Charset.defaultCharset());
+            if (Strings.isNullOrEmpty(key)) {
+                continue;
+            }
+            resultMap.put(key, value);
+        }
+        return resultMap;
+    }
+
+    @Override
+    public LockResult tryLock(String key, long ttl, long timeout) {
+        return this.lock.tryLock(key, ttl, timeout);
+    }
+
+    @Override
+    public LockResult lock(String key, long ttl) {
+        return this.lock.lock(key, ttl);
+    }
+
+    @Override
+    public boolean isLocked(String key) {
+        try {
+            long size = this.client.getKVClient().get(toByteSequence(key))
+                                   .get().getCount();
+
+            if (size > 0) {
+                return true;
+            } else {
+                return false;
+            }

Review Comment:
   return size>0



##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/PdDistributedLock.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.meta.lock;
+
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hugegraph.HugeException;
+import org.apache.hugegraph.pd.client.KvClient;
+import org.apache.hugegraph.pd.common.PDException;
+import org.apache.hugegraph.pd.grpc.kv.LockResponse;
+
+/**
+ * @author zhangyingjie
+ * @date 2022/6/18
+ **/
+public class PdDistributedLock extends AbstractDistributedLock {
+
+    private static int poolSize = 8;
+    private final KvClient client;
+    private ScheduledExecutorService service = new 
ScheduledThreadPoolExecutor(poolSize, r -> {
+        Thread t = new Thread(r);
+        t.setDaemon(true);
+        return t;
+    });
+
+    public PdDistributedLock(KvClient client) {
+        this.client = client;
+    }
+
+    @Override
+    public LockResult lock(String key, long second) {
+        long ttl = second * 1000L;
+        try {
+            LockResponse response = this.client.lock(key, ttl);
+            boolean succeed = response.getSucceed();
+            LockResult result = new LockResult();
+            if (succeed) {
+                result.setLeaseId(response.getClientId());
+                result.lockSuccess(true);
+                long period = ttl - ttl / 4;
+                ScheduledFuture<?> future = service.scheduleAtFixedRate(() -> {
+                    synchronized (result) {

Review Comment:
   这里为啥加锁



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to