1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.comm.netty.handler;
2021import org.apache.giraph.comm.netty.NettyServer;
22import org.apache.giraph.conf.GiraphConstants;
23import org.apache.giraph.utils.IncreasingBitSet;
24import org.apache.hadoop.conf.Configuration;
2526import com.google.common.collect.MapMaker;
2728import java.util.concurrent.ConcurrentMap;
2930/**31 * Provides a thread-safe map for checking worker and request id pairs32 */33publicclassWorkerRequestReservedMap {
34/** Map of the worker ids to the requests received (bit set) */35privatefinal ConcurrentMap<Integer, IncreasingBitSet>
36 workerRequestReservedMap;
3738/**39 * Constructor40 *41 * @param conf Configuration42 */43publicWorkerRequestReservedMap(Configuration conf) {
44 workerRequestReservedMap = new MapMaker().concurrencyLevel(
45 conf.getInt(GiraphConstants.MSG_NUM_FLUSH_THREADS,
46 NettyServer.MAXIMUM_THREAD_POOL_SIZE_DEFAULT)).makeMap();
47 }
4849/**50 * Reserve the request (before the request starts to insure that it is51 * only executed once). We are assuming no failure on the server.52 *53 * @param workerId workerId of the request54 * @param requestId Request id55 * @return True if the reserving succeeded, false otherwise56 */57publicboolean reserveRequest(Integer workerId, long requestId) {
58IncreasingBitSet requestSet = getRequestSet(workerId);
59return requestSet.add(requestId);
60 }
6162/**63 * Get and create the entry as necessary to get the request bit set.64 *65 * @param workerId Id of the worker to get the bit set for66 * @return Bit set for the worker67 */68privateIncreasingBitSet getRequestSet(Integer workerId) {
69IncreasingBitSet requestSet = workerRequestReservedMap.get(workerId);
70if (requestSet == null) {
71 requestSet = newIncreasingBitSet();
72IncreasingBitSet previous =
73 workerRequestReservedMap.putIfAbsent(workerId, requestSet);
74if (previous != null) {
75 requestSet = previous;
76 }
77 }
78return requestSet;
79 }
80 }