Skip to content

Commit

Permalink
renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
patduin committed Apr 17, 2024
1 parent d92cec7 commit 533bd23
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> IGNORABLE_METHODS = Sets.newHashSet("isOpen", "close", "set_ugi", "flushCache");
private String metastoreName;
Expand Down Expand Up @@ -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.");
Expand All @@ -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 {
Expand All @@ -102,7 +102,7 @@ private boolean shouldProceedWithCall(Method method) {
}
return true;
} finally {
meterRegistry.counter(Metrics.CALLS.getMetricName()).increment();
meterRegistry.counter(RateLimitMetrics.CALLS.getMetricName()).increment();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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
Expand All @@ -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));

}

Expand Down

0 comments on commit 533bd23

Please sign in to comment.