forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceGroupService.java
791 lines (692 loc) · 36.1 KB
/
ResourceGroupService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/**
* 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.
*/
package org.apache.pulsar.broker.resourcegroup;
import static com.google.common.base.Preconditions.checkNotNull;
import io.prometheus.client.Counter;
import io.prometheus.client.Summary;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.resourcegroup.ResourceGroup.BytesAndMessagesCount;
import org.apache.pulsar.broker.resourcegroup.ResourceGroup.ResourceGroupMonitoringClass;
import org.apache.pulsar.broker.resourcegroup.ResourceGroup.ResourceGroupRefTypes;
import org.apache.pulsar.broker.service.BrokerService;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.stats.TopicStatsImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The <code>ResourceGroupService</code> contains APIs to manipulate resource groups. It is assumed that there is a
* single instance of the ResourceGroupService on each broker.
* The control plane operations are somewhat stringent, and throw exceptions if (for instance) the op refers to a
* resource group which does not exist.
* The data plane operations (e.g., increment stats) throw exceptions as a last resort; if (e.g.) increment stats
* refers to a non-existent resource group, the stats are quietly not incremented.
*
* @see PulsarService
*/
public class ResourceGroupService {
/**
* Default constructor.
*/
public ResourceGroupService(PulsarService pulsar) {
this.pulsar = pulsar;
this.timeUnitScale = TimeUnit.SECONDS;
this.quotaCalculator = new ResourceQuotaCalculatorImpl();
this.resourceUsageTransportManagerMgr = pulsar.getResourceUsageTransportManager();
this.rgConfigListener = new ResourceGroupConfigListener(this, pulsar);
this.initialize();
}
// For testing only.
public ResourceGroupService(PulsarService pulsar, TimeUnit timescale,
ResourceUsageTopicTransportManager transportMgr,
ResourceQuotaCalculator quotaCalc) {
this.pulsar = pulsar;
this.timeUnitScale = timescale;
this.resourceUsageTransportManagerMgr = transportMgr;
this.quotaCalculator = quotaCalc;
this.rgConfigListener = new ResourceGroupConfigListener(this, pulsar);
this.initialize();
}
protected enum ResourceGroupOpStatus {
OK,
Exists,
DoesNotExist,
NotSupported
}
/**
* Create RG.
*
* @throws if RG with that name already exists.
*/
public void resourceGroupCreate(String rgName, org.apache.pulsar.common.policies.data.ResourceGroup rgConfig)
throws PulsarAdminException {
this.checkRGCreateParams(rgName, rgConfig);
ResourceGroup rg = new ResourceGroup(this, rgName, rgConfig);
resourceGroupsMap.put(rgName, rg);
}
/**
* Create RG, with non-default functions for resource-usage transport-manager.
*
* @throws if RG with that name already exists (even if the resource usage handlers are different).
*/
public void resourceGroupCreate(String rgName,
org.apache.pulsar.common.policies.data.ResourceGroup rgConfig,
ResourceUsagePublisher rgPublisher,
ResourceUsageConsumer rgConsumer) throws PulsarAdminException {
this.checkRGCreateParams(rgName, rgConfig);
ResourceGroup rg = new ResourceGroup(this, rgName, rgConfig, rgPublisher, rgConsumer);
resourceGroupsMap.put(rgName, rg);
}
/**
* Get a copy of the RG with the given name.
*/
public ResourceGroup resourceGroupGet(String resourceGroupName) {
ResourceGroup retrievedRG = this.getResourceGroupInternal(resourceGroupName);
if (retrievedRG == null) {
return null;
}
// Return a copy.
return new ResourceGroup(retrievedRG);
}
/**
* Update RG.
*
* @throws if RG with that name does not exist.
*/
public void resourceGroupUpdate(String rgName, org.apache.pulsar.common.policies.data.ResourceGroup rgConfig)
throws PulsarAdminException {
try {
checkNotNull(rgConfig);
} catch (NullPointerException e) {
throw new IllegalArgumentException("ResourceGroupUpdate: Invalid null ResourceGroup config");
}
ResourceGroup rg = this.getResourceGroupInternal(rgName);
if (rg == null) {
throw new PulsarAdminException("Resource group does not exist: " + rgName);
}
rg.updateResourceGroup(rgConfig);
rgUpdates.labels(rgName).inc();
}
public Set<String> resourceGroupGetAll() {
return resourceGroupsMap.keySet();
}
/**
* Delete RG.
*
* @throws if RG with that name does not exist, or if the RG exists but is still in use.
*/
public void resourceGroupDelete(String name) throws PulsarAdminException {
ResourceGroup rg = this.getResourceGroupInternal(name);
if (rg == null) {
throw new PulsarAdminException("Resource group does not exist: " + name);
}
long tenantRefCount = rg.getResourceGroupNumOfTenantRefs();
long nsRefCount = rg.getResourceGroupNumOfNSRefs();
if ((tenantRefCount + nsRefCount) > 0) {
String errMesg = "Resource group " + name + " still has " + tenantRefCount + " tenant refs";
errMesg += " and " + nsRefCount + " namespace refs on it";
throw new PulsarAdminException(errMesg);
}
rg.resourceGroupPublishLimiter.close();
rg.resourceGroupPublishLimiter = null;
resourceGroupsMap.remove(name);
}
/**
* Get the current number of RGs. For testing.
*/
protected long getNumResourceGroups() {
return resourceGroupsMap.mappingCount();
}
/**
* Registers a tenant as a user of a resource group.
*
* @param resourceGroupName
* @param tenantName
* @throws if the RG does not exist, or if the NS already references the RG.
*/
public void registerTenant(String resourceGroupName, String tenantName) throws PulsarAdminException {
ResourceGroup rg = checkResourceGroupExists(resourceGroupName);
// Check that the tenant-name doesn't already have a RG association.
// [If it does, that should be unregistered before putting a different association.]
ResourceGroup oldRG = this.tenantToRGsMap.get(tenantName);
if (oldRG != null) {
String errMesg = "Tenant " + tenantName + " already references a resource group: " + oldRG.getID();
throw new PulsarAdminException(errMesg);
}
ResourceGroupOpStatus status = rg.registerUsage(tenantName, ResourceGroupRefTypes.Tenants, true,
this.resourceUsageTransportManagerMgr);
if (status == ResourceGroupOpStatus.Exists) {
String errMesg = "Tenant " + tenantName + " already references the resource group " + resourceGroupName;
errMesg += "; this is unexpected";
throw new PulsarAdminException(errMesg);
}
// Associate this tenant name with the RG.
this.tenantToRGsMap.put(tenantName, rg);
rgTenantRegisters.labels(resourceGroupName).inc();
}
/**
* UnRegisters a tenant from a resource group.
*
* @param resourceGroupName
* @param tenantName
* @throws if the RG does not exist, or if the tenant does not references the RG yet.
*/
public void unRegisterTenant(String resourceGroupName, String tenantName) throws PulsarAdminException {
ResourceGroup rg = checkResourceGroupExists(resourceGroupName);
ResourceGroupOpStatus status = rg.registerUsage(tenantName, ResourceGroupRefTypes.Tenants, false,
this.resourceUsageTransportManagerMgr);
if (status == ResourceGroupOpStatus.DoesNotExist) {
String errMesg = "Tenant " + tenantName + " does not yet reference resource group " + resourceGroupName;
throw new PulsarAdminException(errMesg);
}
// Dissociate this tenant name from the RG.
this.tenantToRGsMap.remove(tenantName, rg);
rgTenantUnRegisters.labels(resourceGroupName).inc();
}
/**
* Registers a namespace as a user of a resource group.
*
* @param resourceGroupName
* @param namespaceName
* @throws if the RG does not exist, or if the NS already references the RG.
*/
public void registerNameSpace(String resourceGroupName, String namespaceName) throws PulsarAdminException {
ResourceGroup rg = checkResourceGroupExists(resourceGroupName);
// Check that the NS-name doesn't already have a RG association.
// [If it does, that should be unregistered before putting a different association.]
ResourceGroup oldRG = this.namespaceToRGsMap.get(namespaceName);
if (oldRG != null) {
String errMesg = "Namespace " + namespaceName + " already references a resource group: " + oldRG.getID();
throw new PulsarAdminException(errMesg);
}
ResourceGroupOpStatus status = rg.registerUsage(namespaceName, ResourceGroupRefTypes.Namespaces, true,
this.resourceUsageTransportManagerMgr);
if (status == ResourceGroupOpStatus.Exists) {
String errMesg = String.format("Namespace {} already references the target resource group {}",
namespaceName, resourceGroupName);
throw new PulsarAdminException(errMesg);
}
// Associate this NS-name with the RG.
this.namespaceToRGsMap.put(namespaceName, rg);
rgNamespaceRegisters.labels(resourceGroupName).inc();
}
/**
* Return the resource group associated with a namespace.
*
* @param namespaceName
* @throws if the RG does not exist, or if the NS already references the RG.
*/
public ResourceGroup getNamespaceResourceGroup(String namespaceName) {
return this.namespaceToRGsMap.get(namespaceName);
}
/**
* UnRegisters a namespace from a resource group.
*
* @param resourceGroupName
* @param namespaceName
* @throws if the RG does not exist, or if the NS does not references the RG yet.
*/
public void unRegisterNameSpace(String resourceGroupName, String namespaceName) throws PulsarAdminException {
ResourceGroup rg = checkResourceGroupExists(resourceGroupName);
ResourceGroupOpStatus status = rg.registerUsage(namespaceName, ResourceGroupRefTypes.Namespaces, false,
this.resourceUsageTransportManagerMgr);
if (status == ResourceGroupOpStatus.DoesNotExist) {
String errMesg = String.format("Namespace {} does not yet reference resource group {}",
namespaceName, resourceGroupName);
throw new PulsarAdminException(errMesg);
}
// Dissociate this NS-name from the RG.
this.namespaceToRGsMap.remove(namespaceName, rg);
rgNamespaceUnRegisters.labels(resourceGroupName).inc();
}
/**
* Increments usage stats for the resource groups associated with the given namespace and tenant.
* Expected to be called when a message is produced or consumed on a topic, or when we calculate
* usage periodically in the background by going through broker-service stats. [Not yet decided
* which model we will follow.] Broker-service stats will be cumulative, while calls from the
* topic produce/consume code will be per-produce/consume.
*
* If the tenant and NS are associated with different RGs, the statistics on both RGs are updated.
* If the tenant and NS are associated with the same RG, the stats on the RG are updated only once
* (to avoid a direct double-counting).
* ToDo: will this distinction result in "expected semantics", or shock from users?
* For now, the only caller is internal to this class.
*
* @param tenantName
* @param nsName
* @param monClass
* @param incStats
* @returns true if the stats were updated; false if nothing was updated.
*/
public boolean incrementUsage(String tenantName, String nsName,
ResourceGroupMonitoringClass monClass,
BytesAndMessagesCount incStats) throws PulsarAdminException {
final ResourceGroup nsRG = this.namespaceToRGsMap.get(nsName);
final ResourceGroup tenantRG = this.tenantToRGsMap.get(tenantName);
if (tenantRG == null && nsRG == null) {
return false;
}
// Expect stats to increase monotonically.
if (incStats.bytes < 0 || incStats.messages < 0) {
String errMesg = String.format("incrementUsage on tenant=%s, NS=%s: bytes (%s) or mesgs (%s) is negative",
tenantName, nsName, incStats.bytes, incStats.messages);
throw new PulsarAdminException(errMesg);
}
if (nsRG == tenantRG) {
// Update only once in this case.
// Note that we will update both tenant and namespace RGs in other cases.
nsRG.incrementLocalUsageStats(monClass, incStats);
rgLocalUsageMessages.labels(nsRG.resourceGroupName, monClass.name()).inc(incStats.messages);
rgLocalUsageBytes.labels(nsRG.resourceGroupName, monClass.name()).inc(incStats.bytes);
return true;
}
if (tenantRG != null) {
tenantRG.incrementLocalUsageStats(monClass, incStats);
rgLocalUsageMessages.labels(tenantRG.resourceGroupName, monClass.name()).inc(incStats.messages);
rgLocalUsageBytes.labels(tenantRG.resourceGroupName, monClass.name()).inc(incStats.bytes);
}
if (nsRG != null) {
nsRG.incrementLocalUsageStats(monClass, incStats);
rgLocalUsageMessages.labels(nsRG.resourceGroupName, monClass.name()).inc(incStats.messages);
rgLocalUsageBytes.labels(nsRG.resourceGroupName, monClass.name()).inc(incStats.bytes);
}
return true;
}
// Visibility for testing.
protected BytesAndMessagesCount getRGUsage(String rgName, ResourceGroupMonitoringClass monClass,
boolean getCumulative) throws PulsarAdminException {
final ResourceGroup rg = this.getResourceGroupInternal(rgName);
if (rg != null) {
if (getCumulative) {
return rg.getLocalUsageStatsCumulative(monClass);
}
return rg.getLocalUsageStats(monClass);
}
BytesAndMessagesCount retCount = new BytesAndMessagesCount();
retCount.bytes = -1;
retCount.messages = -1;
return retCount;
}
/**
* Get the RG with the given name. For internal operations only.
*/
private ResourceGroup getResourceGroupInternal(String resourceGroupName) {
try {
checkNotNull(resourceGroupName);
} catch (NullPointerException e) {
throw new IllegalArgumentException("Invalid null resource group name: " + resourceGroupName);
}
return resourceGroupsMap.get(resourceGroupName);
}
private ResourceGroup checkResourceGroupExists(String rgName) throws PulsarAdminException {
ResourceGroup rg = this.getResourceGroupInternal(rgName);
if (rg == null) {
throw new PulsarAdminException("Resource group does not exist: " + rgName);
}
return rg;
}
// Find the difference between the last time stats were updated for this topic, and the current
// time. If the difference is positive, update the stats.
private void updateStatsWithDiff(String topicName, String tenantString, String nsString,
long accByteCount, long accMesgCount, ResourceGroupMonitoringClass monClass) {
ConcurrentHashMap<String, BytesAndMessagesCount> hm;
switch (monClass) {
default:
log.error("updateStatsWithDiff: Unknown monitoring class={}; ignoring", monClass);
return;
case Publish:
hm = this.topicProduceStats;
break;
case Dispatch:
hm = this.topicConsumeStats;
break;
}
BytesAndMessagesCount bmDiff = new BytesAndMessagesCount();
BytesAndMessagesCount bmOldCount;
BytesAndMessagesCount bmNewCount = new BytesAndMessagesCount();
bmNewCount.bytes = accByteCount;
bmNewCount.messages = accMesgCount;
bmOldCount = hm.get(topicName);
if (bmOldCount == null) {
bmDiff.bytes = bmNewCount.bytes;
bmDiff.messages = bmNewCount.messages;
} else {
bmDiff.bytes = bmNewCount.bytes - bmOldCount.bytes;
bmDiff.messages = bmNewCount.messages - bmOldCount.messages;
}
if (bmDiff.bytes <= 0 || bmDiff.messages <= 0) {
return;
}
try {
boolean statsUpdated = this.incrementUsage(tenantString, nsString, monClass, bmDiff);
log.debug("updateStatsWithDiff for topic={}: monclass={} statsUpdated={} for tenant={}, namespace={}; "
+ "by {} bytes, {} mesgs",
topicName, monClass, statsUpdated, tenantString, nsString,
bmDiff.bytes, bmDiff.messages);
hm.put(topicName, bmNewCount);
} catch (Throwable t) {
log.error("updateStatsWithDiff: got ex={} while aggregating for {} side",
t.getMessage(), monClass);
}
}
// Visibility for testing.
protected BytesAndMessagesCount getPublishRateLimiters (String rgName) throws PulsarAdminException {
ResourceGroup rg = this.getResourceGroupInternal(rgName);
if (rg == null) {
throw new PulsarAdminException("Resource group does not exist: " + rgName);
}
return rg.getRgPublishRateLimiterValues();
}
// Visibility for testing.
protected static double getRgQuotaByteCount (String rgName, String monClassName) {
return rgCalculatedQuotaBytes.labels(rgName, monClassName).get();
}
// Visibility for testing.
protected static double getRgQuotaMessageCount (String rgName, String monClassName) {
return rgCalculatedQuotaMessages.labels(rgName, monClassName).get();
}
// Visibility for testing.
protected static double getRgLocalUsageByteCount (String rgName, String monClassName) {
return rgLocalUsageBytes.labels(rgName, monClassName).get();
}
// Visibility for testing.
protected static double getRgLocalUsageMessageCount (String rgName, String monClassName) {
return rgLocalUsageMessages.labels(rgName, monClassName).get();
}
// Visibility for testing.
protected static double getRgUpdatesCount (String rgName) {
return rgUpdates.labels(rgName).get();
}
// Visibility for testing.
protected static double getRgTenantRegistersCount (String rgName) {
return rgTenantRegisters.labels(rgName).get();
}
// Visibility for testing.
protected static double getRgTenantUnRegistersCount (String rgName) {
return rgTenantUnRegisters.labels(rgName).get();
}
// Visibility for testing.
protected static double getRgNamespaceRegistersCount (String rgName) {
return rgNamespaceRegisters.labels(rgName).get();
}
// Visibility for testing.
protected static double getRgNamespaceUnRegistersCount (String rgName) {
return rgNamespaceUnRegisters.labels(rgName).get();
}
// Visibility for testing.
protected static Summary.Child.Value getRgUsageAggregationLatency() {
return rgUsageAggregationLatency.get();
}
// Visibility for testing.
protected static Summary.Child.Value getRgQuotaCalculationTime() {
return rgQuotaCalculationLatency.get();
}
// Periodically aggregate the usage from all topics known to the BrokerService.
// Visibility for unit testing.
protected void aggregateResourceGroupLocalUsages() {
final Summary.Timer aggrUsageTimer = rgUsageAggregationLatency.startTimer();
BrokerService bs = this.pulsar.getBrokerService();
Map<String, TopicStatsImpl> topicStatsMap = bs.getTopicStats();
for (Map.Entry<String, TopicStatsImpl> entry : topicStatsMap.entrySet()) {
String topicName = entry.getKey();
TopicStatsImpl topicStats = entry.getValue();
TopicName topic = TopicName.get(topicName);
String tenantString = topic.getTenant();
String nsString = topic.getNamespacePortion();
if (!this.tenantToRGsMap.containsKey(tenantString) && !this.namespaceToRGsMap.containsKey(nsString)) {
// This topic's NS/tenant are not registered to any RG.
continue;
}
this.updateStatsWithDiff(topicName, tenantString, nsString,
topicStats.getBytesInCounter(), topicStats.getMsgInCounter(),
ResourceGroupMonitoringClass.Publish);
this.updateStatsWithDiff(topicName, tenantString, nsString,
topicStats.getBytesOutCounter(), topicStats.getMsgOutCounter(),
ResourceGroupMonitoringClass.Dispatch);
}
double diffTimeSeconds = aggrUsageTimer.observeDuration();
log.debug("aggregateResourceGroupLocalUsages took {} milliseconds", diffTimeSeconds * 1000);
// Check any re-scheduling requirements for next time.
// Use the same period as getResourceUsagePublishIntervalInSecs;
// cancel and re-schedule this task if the period of execution has changed.
ServiceConfiguration config = pulsar.getConfiguration();
long newPeriodInSeconds = config.getResourceUsageTransportPublishIntervalInSecs();
if (newPeriodInSeconds != this.aggregateLocalUsagePeriodInSeconds) {
if (this.aggreagteLocalUsagePeriodicTask == null) {
log.error("aggregateResourceGroupLocalUsages: Unable to find running task to cancel when "
+ "publish period changed from {} to {} {}",
this.aggregateLocalUsagePeriodInSeconds, newPeriodInSeconds, timeUnitScale);
} else {
boolean cancelStatus = this.aggreagteLocalUsagePeriodicTask.cancel(true);
log.info("aggregateResourceGroupLocalUsages: Got status={} in cancel of periodic "
+ "when publish period changed from {} to {} {}",
cancelStatus, this.aggregateLocalUsagePeriodInSeconds, newPeriodInSeconds, timeUnitScale);
}
this.aggreagteLocalUsagePeriodicTask = pulsar.getExecutor().scheduleAtFixedRate(
this::aggregateResourceGroupLocalUsages,
newPeriodInSeconds,
newPeriodInSeconds,
timeUnitScale);
this.aggregateLocalUsagePeriodInSeconds = newPeriodInSeconds;
}
}
// Periodically calculate the updated quota for all RGs in the background,
// from the reports received from other brokers.
// [Visibility for unit testing.]
protected void calculateQuotaForAllResourceGroups() {
// Calculate the quota for the next window for this RG, based on the observed usage.
final Summary.Timer quotaCalcTimer = rgQuotaCalculationLatency.startTimer();
BytesAndMessagesCount updatedQuota = new BytesAndMessagesCount();
this.resourceGroupsMap.forEach((rgName, resourceGroup) -> {
BytesAndMessagesCount globalUsageStats;
BytesAndMessagesCount localUsageStats;
BytesAndMessagesCount confCounts;
for (ResourceGroupMonitoringClass monClass : ResourceGroupMonitoringClass.values()) {
try {
globalUsageStats = resourceGroup.getGlobalUsageStats(monClass);
localUsageStats = resourceGroup.getLocalUsageStats(monClass);
confCounts = resourceGroup.getConfLimits(monClass);
long[] globUsageBytesArray = new long[String.valueOf(globalUsageStats.bytes).length()];
updatedQuota.bytes = this.quotaCalculator.computeLocalQuota(
confCounts.bytes,
localUsageStats.bytes,
globUsageBytesArray);
long[] globUsageMessagesArray = new long[String.valueOf(globalUsageStats.messages).length()];
updatedQuota.messages = this.quotaCalculator.computeLocalQuota(
confCounts.messages,
localUsageStats.messages,
globUsageMessagesArray);
BytesAndMessagesCount oldBMCount = resourceGroup.updateLocalQuota(monClass, updatedQuota);
rgCalculatedQuotaMessages.labels(rgName, monClass.name()).inc(updatedQuota.messages);
rgCalculatedQuotaBytes.labels(rgName, monClass.name()).inc(updatedQuota.bytes);
long messagesIncrement = updatedQuota.messages - oldBMCount.messages;
long bytesIncrement = updatedQuota.bytes - oldBMCount.bytes;
log.debug("calculateQuota for RG {} [class {}]: bytes incremented by {}, messages by {}",
rgName, monClass, messagesIncrement, bytesIncrement);
} catch (Throwable t) {
log.error("Got exception={} while calculating new quota for monitoring-class={} of RG={}",
t.getMessage(), monClass, rgName);
}
}
});
double diffTimeSeconds = quotaCalcTimer.observeDuration();
log.debug("calculateQuotaForAllResourceGroups took {} milliseconds", diffTimeSeconds * 1000);
// Check any re-scheduling requirements for next time.
// Use the same period as getResourceUsagePublishIntervalInSecs;
// cancel and re-schedule this task if the period of execution has changed.
ServiceConfiguration config = pulsar.getConfiguration();
long newPeriodInSeconds = config.getResourceUsageTransportPublishIntervalInSecs();
if (newPeriodInSeconds != this.resourceUsagePublishPeriodInSeconds) {
if (this.calculateQuotaPeriodicTask == null) {
log.error("calculateQuotaForAllResourceGroups: Unable to find running task to cancel when "
+ "publish period changed from {} to {} {}",
this.resourceUsagePublishPeriodInSeconds, newPeriodInSeconds, timeUnitScale);
} else {
boolean cancelStatus = this.calculateQuotaPeriodicTask.cancel(true);
log.info("calculateQuotaForAllResourceGroups: Got status={} in cancel of periodic "
+ " when publish period changed from {} to {} {}",
cancelStatus, this.resourceUsagePublishPeriodInSeconds, newPeriodInSeconds, timeUnitScale);
}
this.calculateQuotaPeriodicTask = pulsar.getExecutor().scheduleAtFixedRate(
this::calculateQuotaForAllResourceGroups,
newPeriodInSeconds,
newPeriodInSeconds,
timeUnitScale);
this.resourceUsagePublishPeriodInSeconds = newPeriodInSeconds;
maxIntervalForSuppressingReportsMSecs =
this.resourceUsagePublishPeriodInSeconds * MaxUsageReportSuppressRounds;
}
}
private void initialize() {
ServiceConfiguration config = this.pulsar.getConfiguration();
long periodInSecs = config.getResourceUsageTransportPublishIntervalInSecs();
this.aggregateLocalUsagePeriodInSeconds = this.resourceUsagePublishPeriodInSeconds = periodInSecs;
this.aggreagteLocalUsagePeriodicTask = this.pulsar.getExecutor().scheduleAtFixedRate(
this::aggregateResourceGroupLocalUsages,
periodInSecs,
periodInSecs,
this.timeUnitScale);
this.calculateQuotaPeriodicTask = this.pulsar.getExecutor().scheduleAtFixedRate(
this::calculateQuotaForAllResourceGroups,
periodInSecs,
periodInSecs,
this.timeUnitScale);
maxIntervalForSuppressingReportsMSecs =
this.resourceUsagePublishPeriodInSeconds * MaxUsageReportSuppressRounds;
}
private void checkRGCreateParams(String rgName, org.apache.pulsar.common.policies.data.ResourceGroup rgConfig)
throws PulsarAdminException {
try {
checkNotNull(rgConfig);
} catch (NullPointerException e) {
throw new IllegalArgumentException("ResourceGroupCreate: Invalid null ResourceGroup config");
}
if (rgName.isEmpty()) {
throw new IllegalArgumentException("ResourceGroupCreate: can't create resource group with an empty name");
}
ResourceGroup rg = getResourceGroupInternal(rgName);
if (rg != null) {
throw new PulsarAdminException("Resource group already exists:" + rgName);
}
}
private static final Logger log = LoggerFactory.getLogger(ResourceGroupService.class);
@Getter
private final PulsarService pulsar;
protected final ResourceQuotaCalculator quotaCalculator;
private ResourceUsageTransportManager resourceUsageTransportManagerMgr;
// rgConfigListener is used only through its side effects in the ctors, to set up RG/NS loading in config-listeners.
private final ResourceGroupConfigListener rgConfigListener;
// Given a RG-name, get the resource-group
private ConcurrentHashMap<String, ResourceGroup> resourceGroupsMap = new ConcurrentHashMap<>();
// Given a tenant-name, get its associated resource-group
private ConcurrentHashMap<String, ResourceGroup> tenantToRGsMap = new ConcurrentHashMap<>();
// Given a NS-name, get its associated resource-group
private ConcurrentHashMap<String, ResourceGroup> namespaceToRGsMap = new ConcurrentHashMap<>();
// Maps to maintain the usage per topic, in produce/consume directions.
private ConcurrentHashMap<String, BytesAndMessagesCount> topicProduceStats = new ConcurrentHashMap<>();
private ConcurrentHashMap<String, BytesAndMessagesCount> topicConsumeStats = new ConcurrentHashMap<>();
// The task that periodically re-calculates the quota budget for local usage.
private ScheduledFuture<?> aggreagteLocalUsagePeriodicTask;
private long aggregateLocalUsagePeriodInSeconds;
// The task that periodically re-calculates the quota budget for local usage.
private ScheduledFuture<?> calculateQuotaPeriodicTask;
private long resourceUsagePublishPeriodInSeconds;
// Allow a pluggable scale on time units; for testing periodic functionality.
private TimeUnit timeUnitScale;
// The maximum number of successive rounds that we can suppress reporting local usage, because there was no
// substantial change from the prior round. This is to ensure the reporting does not become too chatty.
// Set this value to one more than the cadence of sending reports; e.g., if you want to send every 3rd report,
// set the value to 4.
// Setting this to 0 will make us report in every round.
// Don't set to negative values; behavior will be "undefined".
protected static final int MaxUsageReportSuppressRounds = 5;
// Convenient shorthand, for MaxUsageReportSuppressRounds converted to a time interval in milliseconds.
protected static long maxIntervalForSuppressingReportsMSecs;
// The percentage difference that is considered "within limits" to suppress usage reporting.
// Setting this to 0 will also make us report in every round.
// Don't set it to negative values; behavior will be "undefined".
protected static final float UsageReportSuppressionTolerancePercentage = 5;
// Labels for the various counters used here.
private static final String[] resourceGroupLabel = {"ResourceGroup"};
private static final String[] resourceGroupMonitoringclassLabels = {"ResourceGroup", "MonitoringClass"};
private static final Counter rgCalculatedQuotaBytes = Counter.build()
.name("pulsar_resource_group_calculated_bytes_quota")
.help("Bytes quota calculated for resource group")
.labelNames(resourceGroupMonitoringclassLabels)
.register();
private static final Counter rgCalculatedQuotaMessages = Counter.build()
.name("pulsar_resource_group_calculated_messages_quota")
.help("Messages quota calculated for resource group")
.labelNames(resourceGroupMonitoringclassLabels)
.register();
private static final Counter rgLocalUsageBytes = Counter.build()
.name("pulsar_resource_group_bytes_used")
.help("Bytes locally used within this resource group during the last aggregation interval")
.labelNames(resourceGroupMonitoringclassLabels)
.register();
private static final Counter rgLocalUsageMessages = Counter.build()
.name("pulsar_resource_group_messages_used")
.help("Messages locally used within this resource group during the last aggregation interval")
.labelNames(resourceGroupMonitoringclassLabels)
.register();
private static final Counter rgUpdates = Counter.build()
.name("pulsar_resource_group_updates")
.help("Number of update operations on the given resource group")
.labelNames(resourceGroupLabel)
.register();
private static final Counter rgTenantRegisters = Counter.build()
.name("pulsar_resource_group_tenant_registers")
.help("Number of registrations of tenants")
.labelNames(resourceGroupLabel)
.register();
private static final Counter rgTenantUnRegisters = Counter.build()
.name("pulsar_resource_group_tenant_unregisters")
.help("Number of un-registrations of tenants")
.labelNames(resourceGroupLabel)
.register();
private static final Counter rgNamespaceRegisters = Counter.build()
.name("pulsar_resource_group_namespace_registers")
.help("Number of registrations of namespaces")
.labelNames(resourceGroupLabel)
.register();
private static final Counter rgNamespaceUnRegisters = Counter.build()
.name("pulsar_resource_group_namespace_unregisters")
.help("Number of un-registrations of namespaces")
.labelNames(resourceGroupLabel)
.register();
private static final Summary rgUsageAggregationLatency = Summary.build()
.quantile(0.5, 0.05)
.quantile(0.9, 0.01)
.name("pulsar_resource_group_aggregate_usage_secs")
.help("Time required to aggregate usage of all resource groups, in seconds.")
.register();
private static final Summary rgQuotaCalculationLatency = Summary.build()
.quantile(0.5, 0.05)
.quantile(0.9, 0.01)
.name("pulsar_resource_group_calculate_quota_secs")
.help("Time required to calculate quota of all resource groups, in seconds.")
.register();
}