From 533bd23e6d03b389da42d64bda01e27ddd43c78c Mon Sep 17 00:00:00 2001 From: paduin Date: Wed, 17 Apr 2024 16:45:36 +0200 Subject: [PATCH] renamed --- .../PrefixBasedDatabaseMappingService.java | 2 +- .../extensions/ExtensionBeans.java | 10 +++--- .../client/ratelimit/BucketKeyGenerator.java | 15 ++++++++ .../extensions/client/ratelimit/Metrics.java | 20 ----------- .../client/ratelimit/RateLimitMetrics.java | 35 +++++++++++++++++++ .../ratelimit/RateLimitingClientFactory.java | 4 +-- .../RateLimitingInvocationHandler.java | 16 ++++----- .../ratelimit/BucketKeyGeneratorTest.java | 15 ++++++++ .../RateLimitingInvocationHandlerTest.java | 21 +++++------ 9 files changed, 92 insertions(+), 46 deletions(-) delete mode 100644 waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/Metrics.java create mode 100644 waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitMetrics.java diff --git a/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/mapping/service/impl/PrefixBasedDatabaseMappingService.java b/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/mapping/service/impl/PrefixBasedDatabaseMappingService.java index a2b95f3a..4f7c4f29 100644 --- a/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/mapping/service/impl/PrefixBasedDatabaseMappingService.java +++ b/waggle-dance-core/src/main/java/com/hotels/bdp/waggledance/mapping/service/impl/PrefixBasedDatabaseMappingService.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2016-2023 Expedia, Inc. + * Copyright (C) 2016-2024 Expedia, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/ExtensionBeans.java b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/ExtensionBeans.java index 7fbeeff2..1a7f583a 100644 --- a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/ExtensionBeans.java +++ b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/ExtensionBeans.java @@ -29,6 +29,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import io.github.bucket4j.distributed.ExpirationAfterWriteStrategy; +import io.github.bucket4j.distributed.serialization.Mapper; +import io.github.bucket4j.redis.redisson.cas.RedissonBasedProxyManager; +import io.micrometer.core.instrument.MeterRegistry; + import com.hotels.bdp.waggledance.client.ThriftClientFactory; import com.hotels.bdp.waggledance.extensions.client.ratelimit.BucketBandwidthProvider; import com.hotels.bdp.waggledance.extensions.client.ratelimit.BucketKeyGenerator; @@ -38,11 +43,6 @@ import com.hotels.bdp.waggledance.extensions.client.ratelimit.memory.InMemoryBucketService; import com.hotels.bdp.waggledance.extensions.client.ratelimit.redis.RedisBucketService; -import io.github.bucket4j.distributed.ExpirationAfterWriteStrategy; -import io.github.bucket4j.distributed.serialization.Mapper; -import io.github.bucket4j.redis.redisson.cas.RedissonBasedProxyManager; -import io.micrometer.core.instrument.MeterRegistry; - @Configuration @ConditionalOnProperty(name = "waggledance.extensions.ratelimit.enabled", havingValue = "true") public class ExtensionBeans { diff --git a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGenerator.java b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGenerator.java index 4ac1e647..31ccb2e5 100644 --- a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGenerator.java +++ b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGenerator.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2016-2024 Expedia, Inc. + * + * 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.hotels.bdp.waggledance.extensions.client.ratelimit; public class BucketKeyGenerator { diff --git a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/Metrics.java b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/Metrics.java deleted file mode 100644 index 2c3d7160..00000000 --- a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/Metrics.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.hotels.bdp.waggledance.extensions.client.ratelimit; - -public enum Metrics { - - CALLS("calls"), - EXCEEDED("exceeded"), - ERRORS("errors"); - - private final static String METRIC_BASE_NAME = "com.hotels.bdp.waggledance.extensions.client.ratelimit"; - private String metricName; - - private Metrics(String name) { - this.metricName = METRIC_BASE_NAME + "." + name; - } - - public String getMetricName() { - return metricName; - } - -} diff --git a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitMetrics.java b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitMetrics.java new file mode 100644 index 00000000..1ceef5e3 --- /dev/null +++ b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitMetrics.java @@ -0,0 +1,35 @@ +/** + * Copyright (C) 2016-2024 Expedia, Inc. + * + * 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.hotels.bdp.waggledance.extensions.client.ratelimit; + +public enum RateLimitMetrics { + + CALLS("calls"), + EXCEEDED("exceeded"), + ERRORS("errors"); + + private final static String METRIC_BASE_NAME = "com.hotels.bdp.waggledance.extensions.client.ratelimit"; + private String metricName; + + private RateLimitMetrics(String name) { + this.metricName = METRIC_BASE_NAME + "." + name; + } + + public String getMetricName() { + return metricName; + } + +} diff --git a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingClientFactory.java b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingClientFactory.java index 985874d5..314d1186 100644 --- a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingClientFactory.java +++ b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingClientFactory.java @@ -17,12 +17,12 @@ import java.lang.reflect.Proxy; +import io.micrometer.core.instrument.MeterRegistry; + import com.hotels.bdp.waggledance.api.model.AbstractMetaStore; import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIface; import com.hotels.bdp.waggledance.client.ThriftClientFactory; -import io.micrometer.core.instrument.MeterRegistry; - public class RateLimitingClientFactory implements ThriftClientFactory { private static final Class[] INTERFACES = new Class[] { CloseableThriftHiveMetastoreIface.class }; diff --git a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandler.java b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandler.java index 0a1e8acd..e3d2edb1 100644 --- a/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandler.java +++ b/waggle-dance-extensions/src/main/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandler.java @@ -24,18 +24,18 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.collect.Sets; -import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIface; -import com.hotels.bdp.waggledance.server.WaggleDanceServerException; - import io.github.bucket4j.Bucket; import io.github.bucket4j.ConsumptionProbe; import io.micrometer.core.instrument.MeterRegistry; +import com.google.common.collect.Sets; + +import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIface; +import com.hotels.bdp.waggledance.server.WaggleDanceServerException; + class RateLimitingInvocationHandler implements InvocationHandler { private static Logger log = LoggerFactory.getLogger(RateLimitingInvocationHandler.class); - static final String METRIC_BASE_NAME = "com.hotels.bdp.waggledance.extensions.client.ratelimit"; static final String UNKNOWN_USER = "_UNKNOWN_USER_"; private static final Set IGNORABLE_METHODS = Sets.newHashSet("isOpen", "close", "set_ugi", "flushCache"); private String metastoreName; @@ -76,7 +76,7 @@ private Object doRateLimitCall(CloseableThriftHiveMetastoreIface client, Method if (shouldProceedWithCall(method)) { return doRealCall(client, method, args); } else { - meterRegistry.counter(Metrics.EXCEEDED.getMetricName()).increment(); + meterRegistry.counter(RateLimitMetrics.EXCEEDED.getMetricName()).increment(); log.info("User '{}' made too many requests.", user); // HTTP status would be 429, so using same for Thrift. throw new WaggleDanceServerException("[STATUS=429] Too many requests."); @@ -92,7 +92,7 @@ private boolean shouldProceedWithCall(Method method) { method.getName(), HMSHandler.getThreadLocalIpAddress(), probe.getRemainingTokens(), metastoreName); return probe.isConsumed(); } catch (Exception e) { - meterRegistry.counter(Metrics.ERRORS.getMetricName()).increment(); + meterRegistry.counter(RateLimitMetrics.ERRORS.getMetricName()).increment(); if (log.isDebugEnabled()) { log.error("Error while processing rate limit for: User:{}, method:{}", user, method.getName(), e); } else { @@ -102,7 +102,7 @@ private boolean shouldProceedWithCall(Method method) { } return true; } finally { - meterRegistry.counter(Metrics.CALLS.getMetricName()).increment(); + meterRegistry.counter(RateLimitMetrics.CALLS.getMetricName()).increment(); } } diff --git a/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGeneratorTest.java b/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGeneratorTest.java index cb1007b5..6ad5e82b 100644 --- a/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGeneratorTest.java +++ b/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/BucketKeyGeneratorTest.java @@ -1,3 +1,18 @@ +/** + * Copyright (C) 2016-2024 Expedia, Inc. + * + * 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.hotels.bdp.waggledance.extensions.client.ratelimit; import static org.hamcrest.CoreMatchers.is; diff --git a/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandlerTest.java b/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandlerTest.java index 02a9ba0c..1078e075 100644 --- a/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandlerTest.java +++ b/waggle-dance-extensions/src/test/java/com/hotels/bdp/waggledance/extensions/client/ratelimit/RateLimitingInvocationHandlerTest.java @@ -15,7 +15,6 @@ */ package com.hotels.bdp.waggledance.extensions.client.ratelimit; -import static com.hotels.bdp.waggledance.extensions.client.ratelimit.RateLimitingInvocationHandler.UNKNOWN_USER; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.fail; @@ -24,6 +23,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static com.hotels.bdp.waggledance.extensions.client.ratelimit.RateLimitingInvocationHandler.UNKNOWN_USER; + import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; import org.apache.hadoop.hive.metastore.api.Table; import org.junit.Before; @@ -33,15 +34,15 @@ import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; + import com.hotels.bdp.waggledance.api.model.AbstractMetaStore; import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIface; import com.hotels.bdp.waggledance.client.ThriftClientFactory; import com.hotels.bdp.waggledance.extensions.client.ratelimit.memory.InMemoryBucketService; import com.hotels.bdp.waggledance.server.WaggleDanceServerException; -import io.micrometer.core.instrument.MeterRegistry; -import io.micrometer.core.instrument.simple.SimpleMeterRegistry; - @RunWith(MockitoJUnitRunner.class) public class RateLimitingInvocationHandlerTest { @@ -88,9 +89,9 @@ public void testLimitDifferentUsers() throws Exception { verify(client, times(3)).get_table("db", "table"); verify(client).set_ugi(USER, null); - assertThat(meterRegistry.counter(Metrics.CALLS.getMetricName()).count(), is(4.0)); - assertThat(meterRegistry.counter(Metrics.ERRORS.getMetricName()).count(), is(0.0)); - assertThat(meterRegistry.counter(Metrics.EXCEEDED.getMetricName()).count(), is(1.0)); + assertThat(meterRegistry.counter(RateLimitMetrics.CALLS.getMetricName()).count(), is(4.0)); + assertThat(meterRegistry.counter(RateLimitMetrics.ERRORS.getMetricName()).count(), is(0.0)); + assertThat(meterRegistry.counter(RateLimitMetrics.EXCEEDED.getMetricName()).count(), is(1.0)); } @Test @@ -104,9 +105,9 @@ public void testBucketExceptionStillDoCall() throws Exception { Table result = proxy.get_table("db", "table"); assertThat(result, is(table)); - assertThat(meterRegistry.counter(Metrics.CALLS.getMetricName()).count(), is(1.0)); - assertThat(meterRegistry.counter(Metrics.ERRORS.getMetricName()).count(), is(1.0)); - assertThat(meterRegistry.counter(Metrics.EXCEEDED.getMetricName()).count(), is(0.0)); + assertThat(meterRegistry.counter(RateLimitMetrics.CALLS.getMetricName()).count(), is(1.0)); + assertThat(meterRegistry.counter(RateLimitMetrics.ERRORS.getMetricName()).count(), is(1.0)); + assertThat(meterRegistry.counter(RateLimitMetrics.EXCEEDED.getMetricName()).count(), is(0.0)); }