List of usage examples for com.google.common.util.concurrent RateLimiter create
public static RateLimiter create(double permitsPerSecond)
From source file:com.yahoo.pulsar.client.cli.CmdConsume.java
/** * Run the consume command./*from w ww . j a v a 2s .c o m*/ * * @return 0 for success, < 0 otherwise */ public int run() throws PulsarClientException, IOException { if (mainOptions.size() != 1) throw (new ParameterException("Please provide one and only one topic name.")); if (this.serviceURL == null || this.serviceURL.isEmpty()) throw (new ParameterException("Broker URL is not provided.")); if (this.subscriptionName == null || this.subscriptionName.isEmpty()) throw (new ParameterException("Subscription name is not provided.")); if (this.numMessagesToConsume < 0) throw (new ParameterException("Number of messages should be zero or positive.")); String topic = this.mainOptions.get(0); int numMessagesConsumed = 0; int returnCode = 0; try { ConsumerConfiguration consumerConf = new ConsumerConfiguration(); consumerConf.setSubscriptionType(this.subscriptionType); PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig); Consumer consumer = client.subscribe(topic, this.subscriptionName, consumerConf); RateLimiter limiter = (this.consumeRate > 0) ? RateLimiter.create(this.consumeRate) : null; while (this.numMessagesToConsume == 0 || numMessagesConsumed < this.numMessagesToConsume) { if (limiter != null) { limiter.acquire(); } Message msg = consumer.receive(5, TimeUnit.SECONDS); if (msg == null) { LOG.warn("No message to consume after waiting for 20 seconds."); } else { numMessagesConsumed += 1; System.out.println(MESSAGE_BOUNDARY); String output = this.interpretMessage(msg, displayHex); System.out.println(output); consumer.acknowledge(msg); } } client.close(); } catch (Exception e) { LOG.error("Error while consuming messages"); LOG.error(e.getMessage(), e); returnCode = -1; } finally { LOG.info("{} messages successfully consumed", numMessagesConsumed); } return returnCode; }
From source file:com.googlecode.fascinator.storage.fedora.Fedora36.java
/** * Constructor/*from ww w .j a va2 s . c o m*/ * * @throws StorageException * if any errors occur */ private static void init() throws StorageException { // Don't instantiate twice if (fedoraClient != null) { return; } // Grab all our information from config fedoraUrl = systemConfig.getString(DEFAULT_URL, "storage", "fedora36", "url"); fedoraUsername = systemConfig.getString(null, "storage", "fedora36", "username"); fedoraPassword = systemConfig.getString(null, "storage", "fedora36", "password"); fedoraNamespace = systemConfig.getString(DEFAULT_NAMESPACE, "storage", "fedora36", "namespace"); fedoraTimeout = systemConfig.getInteger(15, "storage", "fedora36", "timeout"); double rateLimit = systemConfig.getInteger(15, "storage", "fedora36", "rateLimit"); requestRateLimiter = RateLimiter.create(rateLimit); if (fedoraUrl == null || fedoraNamespace == null || fedoraUsername == null || fedoraPassword == null) { throw new StorageException("Fedora Storage:" + " Valid Fedora configuration is mising!"); } // Sort out our base URL and HTTP client if (!fedoraUrl.endsWith("/")) { fedoraUrl += "/"; } fedoraGetUrl = fedoraUrl + "get/"; http = new BasicHttpClient(fedoraUrl); http.authenticate(fedoraUsername, fedoraPassword); connections = new HashMap<String, List<GetMethod>>(); // Will throw the StorageException for us if there's something wrong fedoraConnect(); }
From source file:com.jkoolcloud.tnt4j.limiter.LimiterImpl.java
protected void testIdleReset() { long accessTime = System.nanoTime(); if (doLimit && idleReset > 0 && (accessTime - lastAccess.get()) > idleReset) { synchronized (this) { // test again in case multiple threads are attempting at same time // and one of the other threads successfully recreated limiters if ((accessTime - lastAccess.get()) > idleReset) { mpsLimiter = RateLimiter.create(mpsLimiter.getRate()); bpsLimiter = RateLimiter.create(bpsLimiter.getRate()); // record this access time so that other threads blocked at // same time don't also recreate limiters lastAccess.set(accessTime); reset();/*from ww w. java 2 s.c o m*/ } } } long prev = lastAccess.getAndSet(accessTime); if (prev > accessTime) lastAccess.set(prev); }
From source file:org.apache.bookkeeper.client.LedgerHandle.java
LedgerHandle(BookKeeper bk, long ledgerId, LedgerMetadata metadata, DigestType digestType, byte[] password) throws GeneralSecurityException, NumberFormatException { this.bk = bk; this.metadata = metadata; this.pendingAddOps = new ConcurrentLinkedQueue<PendingAddOp>(); if (metadata.isClosed()) { lastAddConfirmed = lastAddPushed = metadata.getLastEntryId(); length = metadata.getLength();//from w ww . jav a 2 s .c o m } else { lastAddConfirmed = lastAddPushed = INVALID_ENTRY_ID; length = 0; } this.ledgerId = ledgerId; if (bk.getConf().getThrottleValue() > 0) { this.throttler = RateLimiter.create(bk.getConf().getThrottleValue()); } else { this.throttler = null; } macManager = DigestManager.instantiate(ledgerId, password, digestType); this.ledgerKey = MacDigestManager.genDigest("ledger", password); distributionSchedule = new RoundRobinDistributionSchedule(metadata.getWriteQuorumSize(), metadata.getAckQuorumSize(), metadata.getEnsembleSize()); ensembleChangeCounter = bk.getStatsLogger().getCounter(BookKeeperClientStats.ENSEMBLE_CHANGES); lacUpdateHitsCounter = bk.getStatsLogger().getCounter(BookKeeperClientStats.LAC_UPDATE_HITS); lacUpdateMissesCounter = bk.getStatsLogger().getCounter(BookKeeperClientStats.LAC_UPDATE_MISSES); bk.getStatsLogger().registerGauge(BookKeeperClientStats.PENDING_ADDS, new Gauge<Integer>() { public Integer getDefaultValue() { return 0; } public Integer getSample() { return pendingAddOps.size(); } }); }
From source file:org.bml.util.alert.impl.EmailAlertHandler.java
/** * @param host//from w w w . ja v a 2 s . co m * @param sender * @param password * @param smtpPort the port the smtp server can be found on (between 0 and 65535) * @param recipients The email recipiants * @param maxRatePerSecond the maximum number of times per second this AlertHandler can be triggered. * @throws IllegalArgumentException if any of the pre-conditions are not met. * * @pre host != null && !host.isEmpty() * @pre sender != null && !sender.isEmpty() * @pre GenericValidator.isInRange(port, NetworkUtils.MIN_IPV4_NETWORK_PORT, NetworkUtils.MAX_IPV4_NETWORK_PORT) * @pre password != null * @pre recipients != null && recipients.length > 0 * @pre maxRatePerSecond > 0 */ public EmailAlertHandler(final String host, final String sender, final String password, final int smtpPort, final String recipients[], double maxRatePerSecond) throws IllegalArgumentException { if (CHECKED) { Preconditions.checkNotNull(host, "host parameter can not be null."); Preconditions.checkArgument(!host.isEmpty(), "host parameter can not be empty."); Preconditions.checkNotNull(sender, "sender parameter can not be null. host=%s", host); Preconditions.checkArgument(!sender.isEmpty(), "sender parameter can not be empty. host=%s", host); Preconditions.checkNotNull(password, "password parameter can not be null. host=%s sender=%s", host, sender); //check e-mail and allow local adresses EmailValidator validator = EmailValidator.getInstance(true); Preconditions.checkArgument(validator.isValid(sender), "sender parameter %s is not a valid e-mail. host=%s", sender, host); Preconditions.checkNotNull(recipients, "recipients parameter can not be null. host=%s sender=%s", host, sender); Preconditions.checkArgument(recipients.length > 0, "recipients parameter can not be zero length. host=%s sender=%s", host, sender); for (int c = 0; c < recipients.length; c++) { Preconditions.checkArgument(validator.isValid(recipients[c]), "recipients entry %s value %s is not a valid e-mail. host=%s sender=%s", c, recipients[c], host, sender); } //check i4 port range... in theory Preconditions.checkArgument( (smtpPort >= NetworkUtils.MIN_IPV4_NETWORK_PORT && smtpPort <= NetworkUtils.MAX_IPV4_NETWORK_PORT), "smtpPort %s is out of range %s - %s", smtpPort, NetworkUtils.MIN_IPV4_NETWORK_PORT, NetworkUtils.MAX_IPV4_NETWORK_PORT); //check rate for RateLimitor Preconditions.checkArgument(maxRatePerSecond > 0, "the maxRatePerSecond parameter must be greater than zero for this AlertHandler implementation to function."); } this.host = host; this.sender = sender; this.password = password; this.smtpPort = smtpPort; this.recipients = Arrays.copyOf(recipients, recipients.length); this.theRateLimiter = RateLimiter.create(maxRatePerSecond); }
From source file:org.apache.pulsar.client.cli.CmdProduce.java
/** * Run the producer.//ww w .j a va2 s.c om * * @return 0 for success, < 0 otherwise * @throws Exception */ public int run() throws PulsarClientException { if (mainOptions.size() != 1) throw (new ParameterException("Please provide one and only one topic name.")); if (this.serviceURL == null || this.serviceURL.isEmpty()) throw (new ParameterException("Broker URL is not provided.")); if (this.numTimesProduce <= 0) throw (new ParameterException("Number of times need to be positive number.")); if (messages.size() == 0 && messageFileNames.size() == 0) throw (new ParameterException("Please supply message content with either --messages or --files")); int totalMessages = (messages.size() + messageFileNames.size()) * numTimesProduce; if (totalMessages > MAX_MESSAGES) { String msg = "Attempting to send " + totalMessages + " messages. Please do not send more than " + MAX_MESSAGES + " messages"; throw new ParameterException(msg); } String topic = this.mainOptions.get(0); int numMessagesSent = 0; int returnCode = 0; try { PulsarClient client = PulsarClient.create(this.serviceURL, this.clientConfig); Producer producer = client.createProducer(topic); List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames); RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null; for (int i = 0; i < this.numTimesProduce; i++) { List<Message> messages = generateMessages(messageBodies); for (Message msg : messages) { if (limiter != null) limiter.acquire(); producer.send(msg); numMessagesSent++; } } client.close(); } catch (Exception e) { LOG.error("Error while producing messages"); LOG.error(e.getMessage(), e); returnCode = -1; } finally { LOG.info("{} messages successfully produced", numMessagesSent); } return returnCode; }
From source file:org.mobicents.protocols.ss7.map.loadService.MapServiceUssdClient.java
public void start() throws Exception { this.logger.info("Starting MapServerUssdClient ..."); this.mapProvider = this.mapStack.getMAPProvider(); this.mapProvider.addMAPDialogListener(this); this.mapProvider.getMAPServiceSupplementary().addMAPServiceListener(this); this.mapProvider.getMAPServiceSupplementary().acivate(); this.rateLimiterObj = RateLimiter.create(getMaxConcurrentDialogs()); // rate SCCP_CLIENT_ADDRESS = new SccpAddressImpl(RoutingIndicator.ROUTING_BASED_ON_DPC_AND_SSN, null, CLIENT_SPC, SSN);/*from w ww. ja v a2 s.c o m*/ SCCP_SERVER_ADDRESS = new SccpAddressImpl(RoutingIndicator.ROUTING_BASED_ON_DPC_AND_SSN, null, SERVER_SPC, SSN); Thread mainThread = new Thread(new MapServerUssdClient_StartClass()); mainThread.start(); this.logger.info("Started MapServerUssdClient ..."); }
From source file:com.yahoo.pulsar.proxy.socket.client.PerformanceClient.java
public void runPerformanceTest(long messages, long limit, int numOfTopic, int sizeOfMessage, String baseUrl, String destination) throws InterruptedException, FileNotFoundException { ExecutorService executor = Executors .newCachedThreadPool(new DefaultThreadFactory("pulsar-perf-producer-exec")); HashMap<String, Tuple> producersMap = new HashMap<>(); String produceBaseEndPoint = baseUrl + destination; for (int i = 0; i < numOfTopic; i++) { String topic = produceBaseEndPoint + "1" + "/"; URI produceUri = URI.create(topic); WebSocketClient produceClient = new WebSocketClient(new SslContextFactory(true)); ClientUpgradeRequest produceRequest = new ClientUpgradeRequest(); SimpleTestProducerSocket produceSocket = new SimpleTestProducerSocket(); try {/*from ww w. j ava 2 s .co m*/ produceClient.start(); produceClient.connect(produceSocket, produceUri, produceRequest); } catch (IOException e1) { log.error("Fail in connecting: [{}]", e1.getMessage()); return; } catch (Exception e1) { log.error("Fail in starting client[{}]", e1.getMessage()); return; } producersMap.put(produceUri.toString(), new Tuple(produceClient, produceRequest, produceSocket)); } // connection to be established TimeUnit.SECONDS.sleep(5); executor.submit(() -> { try { RateLimiter rateLimiter = RateLimiter.create(limit); // Send messages on all topics/producers long totalSent = 0; while (true) { for (String topic : producersMap.keySet()) { if (messages > 0) { if (totalSent++ >= messages) { log.trace("------------------- DONE -----------------------"); Thread.sleep(10000); System.exit(0); } } rateLimiter.acquire(); if (producersMap.get(topic).getSocket().getSession() == null) { Thread.sleep(10000); System.exit(0); } producersMap.get(topic).getSocket().sendMsg((String) String.valueOf(totalSent), sizeOfMessage); messagesSent.increment(); bytesSent.add(1000); } } } catch (Throwable t) { log.error(t.getMessage()); System.exit(0); } }); // Print report stats long oldTime = System.nanoTime(); Histogram reportHistogram = null; String statsFileName = "perf-websocket-producer-" + System.currentTimeMillis() + ".hgrm"; log.info("Dumping latency stats to %s \n", statsFileName); PrintStream histogramLog = new PrintStream(new FileOutputStream(statsFileName), false); HistogramLogWriter histogramLogWriter = new HistogramLogWriter(histogramLog); // Some log header bits histogramLogWriter.outputLogFormatVersion(); histogramLogWriter.outputLegend(); while (true) { try { Thread.sleep(5000); } catch (InterruptedException e) { break; } long now = System.nanoTime(); double elapsed = (now - oldTime) / 1e9; double rate = messagesSent.sumThenReset() / elapsed; double throughput = bytesSent.sumThenReset() / elapsed / 1024 / 1024 * 8; reportHistogram = SimpleTestProducerSocket.recorder.getIntervalHistogram(reportHistogram); log.info( "Throughput produced: {} msg/s --- {} Mbit/s --- Latency: mean: {} ms - med: {} ms - 95pct: {} ms - 99pct: {} ms - 99.9pct: {} ms - 99.99pct: {} ms", throughputFormat.format(rate), throughputFormat.format(throughput), dec.format(reportHistogram.getMean() / 1000.0), dec.format(reportHistogram.getValueAtPercentile(50) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(95) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(99) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(99.9) / 1000.0), dec.format(reportHistogram.getValueAtPercentile(99.99) / 1000.0)); histogramLogWriter.outputIntervalHistogram(reportHistogram); reportHistogram.reset(); oldTime = now; } TimeUnit.SECONDS.sleep(100); executor.shutdown(); }
From source file:google.registry.monitoring.metrics.StackdriverWriter.java
/** * Constructs a {@link StackdriverWriter}. * * <p>The monitoringClient must have read and write permissions to the Cloud Monitoring API v3 on * the provided project.// ww w .java 2 s . c o m */ @Inject public StackdriverWriter(Monitoring monitoringClient, @Named("stackdriverGcpProject") String project, MonitoredResource monitoredResource, @Named("stackdriverMaxQps") int maxQps, @Named("stackdriverMaxPointsPerRequest") int maxPointsPerRequest) { this.monitoringClient = checkNotNull(monitoringClient); this.projectResource = "projects/" + checkNotNull(project); this.monitoredResource = monitoredResource; this.maxPointsPerRequest = maxPointsPerRequest; this.timeSeriesBuffer = new ArrayDeque<>(maxPointsPerRequest); this.rateLimiter = RateLimiter.create(maxQps); }
From source file:org.jclouds.smartos.SmartOSHostController.java
private Iterable<VM> toVMList(String string) { try {//w w w. jav a2 s . com BufferedReader r = new BufferedReader(new StringReader(string)); String line; ImmutableList.Builder<VM> resultBuilder = ImmutableList.builder(); while ((line = r.readLine()) != null) { VM vm = VM.builder().fromVmadmString(line).build(); Map<String, String> ipAddresses; RateLimiter limiter = RateLimiter.create(1.0); for (int i = 0; i < 30; i++) { ipAddresses = getVMIpAddresses(vm.getUuid()); if (!ipAddresses.isEmpty()) { // Got some String ip = ipAddresses.get("net0"); if (ip != null && !ip.equals("0.0.0.0")) { vm = vm.toBuilder().publicAddress(ip).build(); break; } } limiter.acquire(); } resultBuilder.add(vm); } return resultBuilder.build(); } catch (IOException e) { throw new HostException("Error parsing response when building VM list", e); } }