/** * Destroy hazelcast shared timggestamp counter with full id, if it's local then remove the map entry * * @param id of the caller context */ public static void removeTimestampWithFullId(String id) { if(log.isDebugEnabled()) { log.info("REMOVING TIMESTAMP WITH FULL ID " + id); } HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { hazelcastInstance.getAtomicLong(id).destroy(); } else { timestamps.remove(id); } }
/** * Removes the caller and destroy shared params of caller * * @param id The Id of the caller */ public void removeAndDestroyShareParamsOfCaller(String id) { if (id != null) { if(log.isDebugEnabled()) { log.info("REMOVE AND DESTROY OF SHARED PARAM OF CALLER WITH ID " + id); } removeCaller(id); SharedParamManager.removeTimestamp(id); SharedParamManager.removeCounter(id); } }
if (callerContext != null) { callerId = callerContext.getId(); long sharedTimestamp = SharedParamManager.getSharedTimestamp(callerContext.getId()); long sharedNextWindow = sharedTimestamp + callerContext.getUnitTime(); localFirstAccessTime = callerContext.getFirstAccessTime(); callerContext.setFirstAccessTime(sharedTimestamp); callerContext.setNextTimeWindow(sharedNextWindow); callerContext.setGlobalCounter(SharedParamManager.getDistributedCounter(callerId)); if(log.isDebugEnabled()) { log.debug("Setting time windows of caller context when window already set=" + callerId); callerContext.setFirstAccessTime(sharedTimestamp); callerContext.setNextTimeWindow(sharedNextWindow); callerContext.setGlobalCounter(SharedParamManager.getDistributedCounter(callerId)); if (log.isDebugEnabled()) { log.debug("Setting time windows of caller context in intermediate interval=" + SharedParamManager.setSharedTimestamp(callerId, localFirstAccessTime); SharedParamManager.setDistributedCounter(callerId, 0);
if (name.contains(ThrottleConstants.THROTTLE_TIMESTAMP_KEY)) { counterKey = name.split(ThrottleConstants.THROTTLE_TIMESTAMP_KEY)[1]; timestamp = SharedParamManager.getSharedTimestampWithFullId(name); timestamps.put(counterKey, counterKey); log.debug("ADDING TIMESTAMP:============" + counterKey); (removedAtomicTimestampCount < noOfTimestampObjectToBeCleared)) { log.debug("REMOVING TIMESTAMP:============" + counterKey); SharedParamManager.removeTimestampWithFullId(name); removedTimestamps.put(counterKey, counterKey); removedAtomicTimestampCount++; if (removedTimestamps.containsKey(key)) { log.debug("REMOVING COUNTER:============" + key); SharedParamManager.removeCounter(key); removedCounterCount++; if (nonTimestampAssociatedCountersCount < maxNonAssociatedCounterCountToClear) { if (key.contains(ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY)) { SharedParamManager.removeCounter(key); log.debug("NON MATCHING COUNTER:============" + key); nonTimestampAssociatedCountersCount++;
Long distributedCounter = SharedParamManager.asyncGetAndAddDistributedCounter(id, localCounter);
/** * Destroy hazelcast shared timggestamp counter, if it's local then remove the map entry * * @param id of the caller context */ public static void removeTimestamp(String id) { if(log.isDebugEnabled()) { log.debug("REMOVING TIMESTAMP WITH ID " + id); } String key = ThrottleConstants.THROTTLE_TIMESTAMP_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { hazelcastInstance.getAtomicLong(key).destroy(); } else { timestamps.remove(key); } }
/** * Add given value to the distribute counter of caller context of given id. If it's not * distributed return local counter * * @param id of the caller context * @param value to set to the global counter */ public static long addAndGetDistributedCounter(String id, long value) { HazelcastInstance hazelcastInstance = getHazelcastInstance(); id = ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY + id; if(hazelcastInstance != null) { return hazelcastInstance.getAtomicLong(id).addAndGet(value); } else { long currentCount = counters.get(id); long updatedCount = currentCount + value; counters.put(id, updatedCount); return updatedCount; } }
/** * Destroy hazelcast global counter, if it's local then remove the map entry * * @param id of the caller context */ public static void removeCounter(String id) { if(log.isDebugEnabled()) { log.debug("REMOVING COUNTER WITH ID " + id); } id = ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { hazelcastInstance.getAtomicLong(id).destroy(); } else { counters.remove(id); } }
/** * Return hazelcast shared timestamp for this caller context with given id. If it's not distributed will get from the * local counter * * @param id of the shared counter * @return shared hazelcast current shared counter */ public static long getSharedTimestamp(String id) { if(log.isDebugEnabled()) { log.debug("GET TIMESTAMP WITH ID " + id); } String key = ThrottleConstants.THROTTLE_TIMESTAMP_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { return hazelcastInstance.getAtomicLong(key).get(); } else { Long timestamp = timestamps.get(key); if(timestamp != null) { return timestamp; } else { timestamps.put(key, 0L); return 0; } } }
/** * Set distribute counter of caller context of given id to the provided value. If it's not distributed do the same for * local counter * * @param id of the caller context * @param value to set to the global counter */ public static void setDistributedCounter(String id, long value) { if(log.isDebugEnabled()) { log.debug("SETTING COUNTER WITH ID " + id); } id = ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { hazelcastInstance.getAtomicLong(id).set(value); } else { counters.put(id, value); } }
/** * Set distribute timestamp of caller context of given id to the provided value. If it's not distributed do the same for * local counter * * @param id of the caller context * @param timestamp to set to the global counter */ public static void setSharedTimestamp(String id, long timestamp) { if(log.isDebugEnabled()) { log.debug("SETTING TIMESTAMP WITH ID" + id); } String key = ThrottleConstants.THROTTLE_TIMESTAMP_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { hazelcastInstance.getAtomicLong(key).set(timestamp); } else { timestamps.put(id, timestamp); } }
/** * Return hazelcast shared timestamp for this caller context with given full id. If it's not distributed will get from the * local counter * * @param id of the shared counter * @return shared hazelcast current shared counter */ public static long getSharedTimestampWithFullId(String id) { if(log.isDebugEnabled()) { log.info("GET TIMESTAMP WITH FULL ID " + id); } HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { return hazelcastInstance.getAtomicLong(id).get(); } else { Long timestamp = timestamps.get(id); if(timestamp != null) { return timestamp; } else { timestamps.put(id, 0L); return 0; } } }
/** * Return hazelcast shared counter for this caller context with given id. If it's not distributed will get from the * local counter * * @param id of the shared counter * @return shared hazelcast current shared counter */ public static long getDistributedCounter(String id) { if(log.isDebugEnabled()) { log.debug("GET TIMESTAMP WITH ID " + id); } id = ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { return hazelcastInstance.getAtomicLong(id).get(); } else { Long counter = counters.get(id); if(counter != null) { return counter; } else { counters.put(id, 0L); return 0; } } }
/** * Asynchronously add given value to the distribute counter of caller context of given id. If it's not * distributed return local counter. This will return global value before add the provided counter * * @param id of the caller context * @param value to set to the global counter */ public static long asyncGetAndAddDistributedCounter(String id, long value) { if(log.isDebugEnabled()) { log.debug("ASYNC CREATING AND SETTING COUNTER WITH ID " + id); } id = ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY + id; HazelcastInstance hazelcastInstance = getHazelcastInstance(); if(hazelcastInstance != null) { AsyncAtomicLong asyncAtomicLong = (AsyncAtomicLong) hazelcastInstance.getAtomicLong(id); long currentGlobalCounter = asyncAtomicLong.get(); asyncAtomicLong.asyncAddAndGet(value); return currentGlobalCounter; } else { Long currentCount = counters.get(id); if(currentCount == null) { currentCount = 0L; } long updatedCount = currentCount + value; counters.put(id, updatedCount); return currentCount; } }
/** * Asynchronously add given value to the distribute counter of caller context of given id. If it's not * distributed return local counter. This will return global value before add the provided counter * * @param id of the caller context * @param value to set to the global counter */ public static long asyncGetAndAlterDistributedCounter(String id, long value) { HazelcastInstance hazelcastInstance = getHazelcastInstance(); id = ThrottleConstants.THROTTLE_SHARED_COUNTER_KEY + id; if(hazelcastInstance != null) { AsyncAtomicLong asyncAtomicLong = (AsyncAtomicLong) hazelcastInstance.getAtomicLong(id); long currentGlobalCounter = asyncAtomicLong.get(); asyncAtomicLong.asyncAlter(new AddLocalCount(value)); return currentGlobalCounter; } else { Long currentCount = counters.get(id); if(currentCount == null) { currentCount = 0L; } long updatedCount = currentCount + value; counters.put(id, updatedCount); return currentCount; } }