VGalaxies commented on code in PR #2301: URL: https://github.com/apache/incubator-hugegraph/pull/2301#discussion_r1320779976
########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/DistributedLock.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.nio.charset.Charset; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.KV; +import io.etcd.jetcd.Lease; +import io.etcd.jetcd.Lock; + +public class DistributedLock extends AbstractDistributedLock { + + protected static final Logger LOG = Log.logger(DistributedLock.class); + private static final long UNLIMIT_TIMEOUT = -1L; + private final static Object mutex = new Object(); + private static DistributedLock lockProvider = null; + private final KV kvClient; + private final Lock lockClient; + private final Lease leaseClient; + + private DistributedLock(Client client) { + this.kvClient = client.getKVClient(); + this.lockClient = client.getLockClient(); + this.leaseClient = client.getLeaseClient(); + } + + public static DistributedLock getInstance(Client client) { + synchronized (mutex) { + if (null == lockProvider) { + lockProvider = new DistributedLock(client); + } + } + return lockProvider; + } + + private static ByteSequence toByteSequence(String content) { + return ByteSequence.from(content, Charset.defaultCharset()); + } + + public LockResult tryLock(String lockName, long ttl, long timeout) { + LockResult lockResult = new LockResult(); + ScheduledExecutorService service = + Executors.newSingleThreadScheduledExecutor(); Review Comment: 参考 PdDistributedLock 进行了修改 ########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/meta/lock/DistributedLock.java: ########## @@ -0,0 +1,164 @@ +/* + * 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.nio.charset.Charset; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.apache.hugegraph.util.Log; +import org.slf4j.Logger; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.KV; +import io.etcd.jetcd.Lease; +import io.etcd.jetcd.Lock; + +public class DistributedLock extends AbstractDistributedLock { + + protected static final Logger LOG = Log.logger(DistributedLock.class); + private static final long UNLIMIT_TIMEOUT = -1L; + private final static Object mutex = new Object(); + private static DistributedLock lockProvider = null; + private final KV kvClient; + private final Lock lockClient; + private final Lease leaseClient; + + private DistributedLock(Client client) { + this.kvClient = client.getKVClient(); + this.lockClient = client.getLockClient(); + this.leaseClient = client.getLeaseClient(); + } + + public static DistributedLock getInstance(Client client) { + synchronized (mutex) { + if (null == lockProvider) { + lockProvider = new DistributedLock(client); + } + } + return lockProvider; + } + + private static ByteSequence toByteSequence(String content) { + return ByteSequence.from(content, Charset.defaultCharset()); + } + + public LockResult tryLock(String lockName, long ttl, long timeout) { + LockResult lockResult = new LockResult(); + ScheduledExecutorService service = + Executors.newSingleThreadScheduledExecutor(); Review Comment: 参考 PdDistributedLock 进行了修改 ########## 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); Review Comment: done -- 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]
