qqu0127 commented on code in PR #2338: URL: https://github.com/apache/helix/pull/2338#discussion_r1068397632
########## zookeeper-api/src/main/java/org/apache/helix/zookeeper/zkclient/ListenerContainer.java: ########## @@ -0,0 +1,186 @@ +package org.apache.helix.zookeeper.zkclient; + +/* + * 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 com.google.common.annotations.VisibleForTesting; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.locks.ReentrantLock; +import java.util.function.Consumer; + + +/** + * A thread-safe container wrapper class for Listeners registered to ZkClient. + * It stores 2 types of listener separately, one-time listener and persistent listener. The former ones are removed + * right after its consumption, while the latter need to be explicitly removed. + * @param <T> the type of listener + */ +public class ListenerContainer<T> { + private final ReentrantLock _lock = new ReentrantLock(true); + private final Map<String, Set<T>> _persistentListener = new ConcurrentHashMap<>(); Review Comment: ConcurrentHashMap has instance level thread-safety, but it doesn't guarantee thread-safety in its value set. The lock is required, you can see https://github.com/apache/helix/pull/2338#discussion_r1068357047 -- 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]
