xyuanlu commented on code in PR #2515: URL: https://github.com/apache/helix/pull/2515#discussion_r1214642055
########## 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); + } Review Comment: If there is already a entry on that path, then constructor still succeed but no actual entry is created. We should differentiate these 2 cases. Another thing to consider is that, both consumer and producer is going to use this client to create semaphore and acquire permit. So do we want to have entry creation integrated in the constructor? -- 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]
