forked from bifromqio/bifromq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Correct Setting's caching behavior
2. Add a webhook based setting provider for demo
- Loading branch information
Showing
14 changed files
with
580 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...lper/src/main/java/com/baidu/bifromq/plugin/settingprovider/MonitoredSettingProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2024. The BifroMQ Authors. All Rights Reserved. | ||
* | ||
* Licensed 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 com.baidu.bifromq.plugin.settingprovider; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.Metrics; | ||
import io.micrometer.core.instrument.Timer; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class MonitoredSettingProvider implements ISettingProvider { | ||
private final Timer provideCallTimer; | ||
private final Counter provideCallErrorCounter; | ||
private final ISettingProvider delegate; | ||
|
||
public MonitoredSettingProvider(ISettingProvider delegate) { | ||
this.delegate = delegate; | ||
provideCallTimer = Timer.builder("call.exec.timer") | ||
.tag("method", "SettingProvider/provide") | ||
.tag("type", delegate.getClass().getName()) | ||
.register(Metrics.globalRegistry); | ||
provideCallErrorCounter = Counter.builder("call.exec.fail.count") | ||
.tag("method", "SettingProvider/provide") | ||
.tag("type", delegate.getClass().getName()) | ||
.register(Metrics.globalRegistry); | ||
} | ||
|
||
@Override | ||
public <R> R provide(Setting setting, String tenantId) { | ||
try { | ||
Timer.Sample sample = Timer.start(); | ||
R newVal = delegate.provide(setting, tenantId); | ||
sample.stop(provideCallTimer); | ||
if (setting.isValid(newVal)) { | ||
return newVal; | ||
} else { | ||
log.warn("Invalid setting value: setting={}, value={}", setting.name(), newVal); | ||
provideCallErrorCounter.increment(); | ||
return null; | ||
} | ||
} catch (Throwable e) { | ||
log.error("Setting provider throws exception: setting={}", setting.name(), e); | ||
provideCallErrorCounter.increment(); | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
Metrics.globalRegistry.remove(provideCallTimer); | ||
Metrics.globalRegistry.remove(provideCallErrorCounter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
.../src/test/java/com/baidu/bifromq/plugin/settingprovider/MonitoredSettingProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2024. The BifroMQ Authors. All Rights Reserved. | ||
* | ||
* Licensed 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 com.baidu.bifromq.plugin.settingprovider; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNull; | ||
|
||
import io.micrometer.core.instrument.Metrics; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import lombok.SneakyThrows; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
public class MonitoredSettingProviderTest { | ||
@Mock | ||
private ISettingProvider provider; | ||
|
||
private SimpleMeterRegistry registry; | ||
private AutoCloseable closeable; | ||
|
||
@BeforeMethod | ||
public void setup() { | ||
registry = new SimpleMeterRegistry(); | ||
Metrics.addRegistry(registry); | ||
closeable = MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@SneakyThrows | ||
@AfterMethod | ||
public void tearDown() { | ||
Metrics.removeRegistry(registry); | ||
closeable.close(); | ||
} | ||
|
||
@Test | ||
public void provideValidValue() { | ||
MonitoredSettingProvider monitoredSettingProvider = new MonitoredSettingProvider(provider); | ||
String tenantId = "tenantA"; | ||
when(provider.provide(Setting.MaxTopicLevels, tenantId)).thenReturn(64); | ||
int levels = monitoredSettingProvider.provide(Setting.MaxTopicLevels, tenantId); | ||
assertEquals(levels, 64); | ||
verify(provider).provide(Setting.MaxTopicLevels, tenantId); | ||
assertEquals(registry.get("call.exec.timer").timer().count(), 1); | ||
} | ||
|
||
@Test | ||
public void provideInvalidValue() { | ||
MonitoredSettingProvider monitoredSettingProvider = new MonitoredSettingProvider(provider); | ||
String tenantId = "tenantA"; | ||
when(provider.provide(Setting.MaxTopicLevels, tenantId)).thenReturn(new Object()); | ||
assertNull(monitoredSettingProvider.provide(Setting.MaxTopicLevels, tenantId)); | ||
verify(provider).provide(Setting.MaxTopicLevels, tenantId); | ||
assertEquals(registry.get("call.exec.timer").timer().count(), 1); | ||
assertEquals(registry.get("call.exec.fail.count").counter().count(), 1); | ||
} | ||
|
||
@Test | ||
public void provideNullValue() { | ||
MonitoredSettingProvider monitoredSettingProvider = new MonitoredSettingProvider(provider); | ||
String tenantId = "tenantA"; | ||
when(provider.provide(Setting.MaxTopicLevels, tenantId)).thenReturn(null); | ||
assertNull(monitoredSettingProvider.provide(Setting.MaxTopicLevels, tenantId)); | ||
verify(provider).provide(Setting.MaxTopicLevels, tenantId); | ||
assertEquals(registry.get("call.exec.timer").timer().count(), 1); | ||
assertEquals(registry.get("call.exec.fail.count").counter().count(), 1); | ||
} | ||
|
||
@Test | ||
public void provideThrows() { | ||
MonitoredSettingProvider monitoredSettingProvider = new MonitoredSettingProvider(provider); | ||
String tenantId = "tenantA"; | ||
when(provider.provide(Setting.MaxTopicLevels, tenantId)).thenThrow(new RuntimeException("Mocked exception")); | ||
assertNull(monitoredSettingProvider.provide(Setting.MaxTopicLevels, tenantId)); | ||
verify(provider).provide(Setting.MaxTopicLevels, tenantId); | ||
assertEquals(registry.get("call.exec.fail.count").counter().count(), 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.