-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathTestReactiveBlacklisting.java
More file actions
124 lines (108 loc) · 4.73 KB
/
TestReactiveBlacklisting.java
File metadata and controls
124 lines (108 loc) · 4.73 KB
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
package net.opentsdb.tsd;
import com.stumbleupon.async.Deferred;
import net.opentsdb.core.BaseTsdbTest;
import net.opentsdb.core.Query;
import net.opentsdb.core.TSDB;
import net.opentsdb.meta.TestTSUIDQuery;
import net.opentsdb.storage.MockBase;
import net.opentsdb.uid.UniqueId;
import net.opentsdb.utils.BlacklistManager;
import net.opentsdb.utils.Config;
import net.opentsdb.utils.DateTime;
import org.hbase.async.HBaseClient;
import org.hbase.async.KeyValue;
import org.hbase.async.Scanner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ TSDB.class, HBaseClient.class, Config.class, HttpQuery.class,
Query.class, Deferred.class, UniqueId.class, DateTime.class, KeyValue.class,
Scanner.class })
public class TestReactiveBlacklisting extends BaseTsdbTest {
private QueryRpc rpc;
private Map<String, String> host1tags;
private Map<String, String> host2tags;
private final int rowCountThreshold = 0;
private final int blockTimeInSeconds = 10;
private final String allHostQuery = "/api/query?start=1h-ago&m=sum:sys.cpu.user";
private final String host1Query1 = "/api/query?start=1h-ago&m=sum:sys.cpu.user{host=web01,datacenter=dc01}";
private final String host1Query2 = "/api/query?start=1h-ago&m=sum:sys.cpu.user{datacenter=dc01,host=web01}";
private final String host2Query1 = "/api/query?start=1h-ago&m=sum:sys.cpu.user{host=web02,datacenter=dc01}";
@Before
public void beforeLocal() throws Exception {
Whitebox.setInternalState(config, "enable_tsuid_incrementing", true);
Whitebox.setInternalState(config, "enable_realtime_ts", true);
rpc = new QueryRpc();
storage = new MockBase(tsdb, client, true, true, true, true);
TestTSUIDQuery.setupStorage(tsdb, storage);
host1tags = new HashMap<String, String>();
host1tags.put("host", "web01");
host1tags.put("owner", "web02");
host2tags = new HashMap<String, String>();
host2tags.put("host", "web02");
host2tags.put("owner", "web01");
}
private void executeQueryWith200Response(String queryString) throws IOException {
final HttpQuery query = NettyMocks.getQuery(tsdb, queryString);
rpc.execute(tsdb, query);
assert (query.response().getStatus().getCode() == 200);
}
private void executeQueryWith400Blacklist(String queryString) throws IOException {
final HttpQuery query = NettyMocks.getQuery(tsdb, queryString);
boolean exception = false;
try {
rpc.execute(tsdb, query);
} catch (BadRequestException be) {
exception = true;
assert (be.getMessage().startsWith("Metric Blacklisted"));
}
assert (exception == true);
}
private void toggleReactiveBlacklisting(boolean enable) {
BlacklistManager.initBlockListConfiguration(enable, rowCountThreshold, blockTimeInSeconds);
}
@Test
public void testWithNoBlacklisting() throws Exception {
toggleReactiveBlacklisting(false);
PowerMockito.mockStatic(DateTime.class);
PowerMockito.when(DateTime.currentTimeMillis()).thenReturn(1461924148000L);
tsdb.addPoint("sys.cpu.user", 1461924131, 1, host1tags);
tsdb.addPoint("sys.cpu.user", 1461924141, 1, host1tags);
tsdb.addPoint("sys.cpu.user", 1461924131, 1, host2tags);
tsdb.addPoint("sys.cpu.user", 1461924141, 1, host2tags);
executeQueryWith200Response(allHostQuery);
executeQueryWith200Response(allHostQuery);
executeQueryWith200Response(host1Query1);
executeQueryWith200Response(host1Query2);
executeQueryWith200Response(host2Query1);
Thread.sleep(blockTimeInSeconds * 1000);
executeQueryWith200Response(allHostQuery);
executeQueryWith200Response(host1Query2);
}
@Test
public void testWithBlacklisting() throws Exception {
toggleReactiveBlacklisting(true);
PowerMockito.mockStatic(DateTime.class);
PowerMockito.when(DateTime.currentTimeMillis()).thenReturn(1461924148000L);
tsdb.addPoint("sys.cpu.user", 1461924131, 1, host1tags);
tsdb.addPoint("sys.cpu.user", 1461924141, 1, host1tags);
tsdb.addPoint("sys.cpu.user", 1461924131, 1, host2tags);
tsdb.addPoint("sys.cpu.user", 1461924141, 1, host2tags);
executeQueryWith200Response(allHostQuery);
executeQueryWith400Blacklist(allHostQuery);
executeQueryWith200Response(host1Query1);
executeQueryWith400Blacklist(host1Query2);
executeQueryWith200Response(host2Query1);
Thread.sleep(blockTimeInSeconds * 1000);
executeQueryWith200Response(allHostQuery);
executeQueryWith200Response(host1Query2);
}
}