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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch.test.fs4mock;
import com.yahoo.fs4.mplex.Backend;
import com.yahoo.prelude.fastsearch.FS4ResourcePool;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author bratseth
*/
public class MockFS4ResourcePool extends FS4ResourcePool {
private final Map<String, Integer> requestsPerBackend = new HashMap<>();
private final Set<String> nonRespondingBackends = new HashSet<>();
private final Map<String, Long> activeDocumentsInBackend = new HashMap<>();
private final long testingThreadId;
public MockFS4ResourcePool() {
super("container.0", 1);
this.testingThreadId = Thread.currentThread().getId();
}
@Override
public Backend getBackend(String hostname, int port) {
countRequest(hostname + ":" + port);
if (nonRespondingBackends.contains(hostname))
return new MockBackend(hostname, 0L, false);
else
return new MockBackend(hostname, activeDocumentsInBackend.getOrDefault(hostname, 0L), true);
}
/**
* Returns the number of times a backend for this hostname and port has been requested
* from the thread creating this
*/
public int requestCount(String hostname, int port) {
return requestsPerBackend.getOrDefault(hostname + ":" + port, 0);
}
/** Sets the number of active documents the given host will report to have in ping responses */
public void setActiveDocuments(String hostname, long activeDocuments) {
activeDocumentsInBackend.put(hostname, activeDocuments);
}
private void countRequest(String hostAndPort) {
// ignore requests from the ping thread to avoid timing issues
if (Thread.currentThread().getId() != testingThreadId) return;
requestsPerBackend.put(hostAndPort, requestsPerBackend.getOrDefault(hostAndPort, 0) + 1);
}
public void setResponding(String hostname, boolean responding) {
if (responding)
nonRespondingBackends.remove(hostname);
else
nonRespondingBackends.add(hostname);
}
}
|