1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * 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 and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.dmf.mr.beans;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
27 import com.att.nsa.drumlin.till.nv.rrNvReadable;
28 import com.att.nsa.drumlin.till.nv.rrNvReadable.invalidSettingValue;
29 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
30 import com.att.nsa.metrics.impl.CdmRateTicker;
31 import org.onap.dmaap.dmf.mr.CambriaApiException;
32 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
33 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
34 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
35 import org.onap.dmaap.dmf.mr.utils.Utils;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.stereotype.Component;
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.concurrent.TimeUnit;
45 * class provide rate information
47 * @author anowarul.islam
51 public class DMaaPCambriaLimiter {
52 private final HashMap<String, RateInfo> fRateInfo;
53 private final double fMaxEmptyPollsPerMinute;
54 private final double fMaxPollsPerMinute;
55 private final int fWindowLengthMins;
56 private final long fSleepMs;
57 private final long fSleepMs1;
58 private static final EELFLogger log = EELFManager.getInstance().getLogger(DMaaPCambriaLimiter.class);
61 * constructor initializes
64 * @throws missingReqdSetting
65 * @throws invalidSettingValue
68 public DMaaPCambriaLimiter(@Qualifier("propertyReader") rrNvReadable settings) {
69 fRateInfo = new HashMap<>();
70 fMaxEmptyPollsPerMinute = settings.getDouble(CambriaConstants.kSetting_MaxEmptyPollsPerMinute,
71 CambriaConstants.kDefault_MaxEmptyPollsPerMinute);
72 fMaxPollsPerMinute = settings.getDouble(CambriaConstants.kSetting_MaxPollsPerMinute,
74 fWindowLengthMins = settings.getInt(CambriaConstants.kSetting_RateLimitWindowLength,
75 CambriaConstants.kDefault_RateLimitWindowLength);
76 fSleepMs = settings.getLong(CambriaConstants.kSetting_SleepMsOnRateLimit,
77 CambriaConstants.kDefault_SleepMsOnRateLimit);
78 fSleepMs1 = settings.getLong(CambriaConstants.kSetting_SleepMsRealOnRateLimit,
84 * Construct a rate limiter.
86 * @param maxEmptyPollsPerMinute
87 * Pass <= 0 to deactivate rate limiting.
88 * @param windowLengthMins
90 public DMaaPCambriaLimiter(double maxEmptyPollsPerMinute, double maxPollsPerMinute,int windowLengthMins) {
91 this(maxEmptyPollsPerMinute,maxPollsPerMinute, windowLengthMins, getSleepMsForRate(maxEmptyPollsPerMinute),getSleepMsForRate(1));
95 * Construct a rate limiter
97 * @param maxEmptyPollsPerMinute
98 * Pass <= 0 to deactivate rate limiting.
100 * @param windowLengthMins
102 public DMaaPCambriaLimiter(double maxEmptyPollsPerMinute,double maxPollsPerMinute, int windowLengthMins, long sleepMs ,long sleepMS1) {
103 fRateInfo = new HashMap<>();
104 fMaxEmptyPollsPerMinute = Math.max(0, maxEmptyPollsPerMinute);
105 fMaxPollsPerMinute = Math.max(0, maxPollsPerMinute);
106 fWindowLengthMins = windowLengthMins;
107 fSleepMs = Math.max(0, sleepMs);
108 fSleepMs1 = Math.max(0, sleepMS1);
112 * static method provide the sleep time
114 * @param ratePerMinute
117 public static long getSleepMsForRate(double ratePerMinute) {
118 if (ratePerMinute <= 0.0)
120 return Math.max(1000, Math.round(60 * 1000 / ratePerMinute));
124 * Tell the rate limiter about a call to a topic/group/id. If the rate is
125 * too high, this call delays its return and throws an exception.
128 * @param consumerGroup
130 * @throws CambriaApiException
132 public void onCall(String topic, String consumerGroup, String clientId,String remoteHost) throws CambriaApiException {
133 // do nothing if rate is configured 0 or less
134 if (fMaxEmptyPollsPerMinute <= 0) {
137 // setup rate info for this tuple
138 final RateInfo ri = getRateInfo(topic, consumerGroup, clientId);
139 final double rate = ri.onCall();
140 log.info(ri.getLabel() + ": " + rate + " empty replies/minute.");
141 if (rate > fMaxEmptyPollsPerMinute) {
143 log.warn(ri.getLabel() + ": " + rate + " empty replies/minute, limit is " + fMaxPollsPerMinute
146 log.warn(ri.getLabel() + ": " + "Slowing response with " + fSleepMs
147 + " ms sleep, then responding in error.");
148 Thread.sleep(fSleepMs);
151 log.info(ri.getLabel() + ": " + "No sleep configured, just throwing error.");
153 } catch (InterruptedException e) {
154 log.error("Exception "+ e);
155 Thread.currentThread().interrupt();
159 ErrorResponse errRes = new ErrorResponse(HttpStatusCodes.k429_tooManyRequests,
160 DMaaPResponseCode.TOO_MANY_REQUESTS.getResponseCode(),
161 "This client is making too many requests. Please use a long poll "
162 + "setting to decrease the number of requests that result in empty responses. ","",Utils.getFormattedDate(new Date()),topic,"","",consumerGroup+"/"+clientId,remoteHost);
164 log.info(errRes.toString());
165 throw new CambriaApiException(errRes);
174 * @param consumerGroup
178 public void onSend(String topic, String consumerGroup, String clientId, long sentCount) {
179 // check for good replies
181 // that was a good send, reset the metric
182 getRateInfo(topic, consumerGroup, clientId).reset();
186 private static class RateInfo {
187 private final String fLabel;
188 private final CdmRateTicker fCallRateSinceLastMsgSend;
190 * constructor initialzes
193 * @param windowLengthMinutes
195 public RateInfo(String label, int windowLengthMinutes) {
197 fCallRateSinceLastMsgSend = new CdmRateTicker("Call rate since last msg send", 1, TimeUnit.MINUTES,
198 windowLengthMinutes, TimeUnit.MINUTES);
201 public String getLabel() {
206 * CdmRateTicker is reset
208 public void reset() {
209 fCallRateSinceLastMsgSend.reset();
216 public double onCall() {
217 fCallRateSinceLastMsgSend.tick();
218 return fCallRateSinceLastMsgSend.getRate();
228 private RateInfo getRateInfo(String topic, String consumerGroup, String clientId) {
229 final String key = makeKey(topic, consumerGroup, clientId);
230 RateInfo ri = fRateInfo.get(key);
232 ri = new RateInfo(key, fWindowLengthMins);
233 fRateInfo.put(key, ri);
244 private String makeKey(String topic, String group, String id) {
245 return topic + "::" + group + "::" + id;