You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by "Marcosrico (via GitHub)" <gi...@apache.org> on 2023/06/02 01:16:11 UTC

[GitHub] [helix] Marcosrico commented on a diff in pull request #2515: Distributed Semaphore Implementation

Marcosrico commented on code in PR #2515:
URL: https://github.com/apache/helix/pull/2515#discussion_r1213816608


##########
meta-client/src/main/java/org/apache/helix/metaclient/recipes/lock/DistributedSemaphore.java:
##########
@@ -0,0 +1,174 @@
+package org.apache.helix.metaclient.recipes.lock;
+
+/*
+ * 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.
+ */
+
+import org.apache.helix.metaclient.api.MetaClientInterface;
+import org.apache.helix.metaclient.datamodel.DataRecord;
+import org.apache.helix.metaclient.exception.MetaClientException;
+import org.apache.helix.metaclient.factories.MetaClientConfig;
+import org.apache.helix.metaclient.impl.zk.factory.ZkMetaClientConfig;
+import org.apache.helix.metaclient.impl.zk.factory.ZkMetaClientFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.concurrent.TimeUnit;
+
+public class DistributedSemaphore {
+  private final MetaClientInterface<DataRecord> _metaClient;
+  private final String _path;
+  private static final String INITIAL_CAPACITY_NAME = "INITIAL_CAPACITY";
+  private static final String REMAINING_CAPACITY_NAME = "REMAINING_CAPACITY";
+  private static final long DEFAULT_REMAINING_CAPACITY = -1;
+  private static final Logger LOG = LoggerFactory.getLogger(DistributedSemaphore.class);
+
+  public DistributedSemaphore(MetaClientConfig config, String path, int capacity) {
+    if (MetaClientConfig.StoreType.ZOOKEEPER.equals(config.getStoreType())) {
+      ZkMetaClientConfig zkMetaClientConfig = new ZkMetaClientConfig.ZkMetaClientConfigBuilder()
+          .setConnectionAddress(config.getConnectionAddress())
+          .setZkSerializer(new DataRecordSerializer()) // Currently only support ZNRecordSerializer.
+          // Setting DataRecordSerializer as DataRecord extends ZNRecord.
+          .build();
+      _metaClient = new ZkMetaClientFactory().getMetaClient(zkMetaClientConfig);
+      _metaClient.connect();
+    } else {
+      throw new MetaClientException("Unsupported store type: " + config.getStoreType());
+    }
+
+    _path = path;
+    if (_metaClient.exists(path) == null) {
+      DataRecord dataRecord = new DataRecord(path);
+      dataRecord.setLongField(INITIAL_CAPACITY_NAME, capacity);
+      dataRecord.setLongField(REMAINING_CAPACITY_NAME, capacity);
+      _metaClient.create(path, dataRecord);
+    }
+  }
+
+  public DistributedSemaphore(MetaClientInterface<DataRecord> client, String path) {
+    _metaClient = client;
+    _path = path;
+    _metaClient.connect();
+  }
+
+  /**
+   * Acquire a permit. If no permit is available, block until when it was able to acquire.
+   * @return a permit
+   */
+  public Permit acquire() {
+    if (getRemainingCapacity() > 0) {
+      updateAcquirePermit(1);
+      return retrievePermit(_path);
+    } else {
+      throw new MetaClientException("No sufficient permits available");
+    }
+  }
+
+
+  /**
+   * Try to acquire a permit. If not enough permit is available, wait for a specific time or return when it was able to acquire.
+   * @param count number of permits to acquire
+   * @return a collection of permits
+   */
+  public Collection<Permit> acquire(int count) {
+    if (getRemainingCapacity() < count) {
+      throw new MetaClientException("No sufficient permits available");
+    } else {
+      updateAcquirePermit(count);
+      Collection<Permit> permits = new ArrayList<>();
+      for (int i = 0; i < count; i++) {
+        permits.add(retrievePermit(_path));
+      }
+      return permits;
+    }
+  }
+
+  /**
+   * Try to acquire a permit. If no enough permit is available, wait for a specific time or return when it was able to acquire.

Review Comment:
   Oh i see, then I must have gotten both acquire and tryAcquire mixed up. Should acquire then be blocking?



-- 
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: reviews-unsubscribe@helix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org