Skip to content

Commit

Permalink
Format files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamyki committed Dec 6, 2023
1 parent d44ac9c commit 10b8af2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,15 +575,17 @@ public MemcacheClientBuilder<V> withUsernamePassword(
}

/**
* When using ketama client use provided NodeLocator to find client for given key.
* NodeLocator will be recreated when clients appear and disappear when using dynamic resolver.
* When using ketama client use provided NodeLocator to find client for given key. NodeLocator
* will be recreated when clients appear and disappear when using dynamic resolver.
*
* <p> This option can be used to change default hashing algorithm or vnode_ratio in consistent hashing algorithm.
* <p>This option can be used to change default hashing algorithm or vnode_ratio in consistent
* hashing algorithm.
*
* @param nodeLocator mapper from client collection to NodeLocator
* @return itself
*/
public MemcacheClientBuilder<V> withNodeLocator(Function<Collection<AddressAndClient>, NodeLocator> nodeLocator) {
public MemcacheClientBuilder<V> withNodeLocator(
Function<Collection<AddressAndClient>, NodeLocator> nodeLocator) {
this.nodeLocator = nodeLocator;
return this;
}
Expand Down Expand Up @@ -718,7 +720,7 @@ private RawMemcacheClient createResolvingClient(
input -> createClient(input, binary, authenticator),
shutdownDelay,
TimeUnit.MILLISECONDS,
nodeLocator);
nodeLocator);

client.start();
return client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Continuum implements NodeLocator {
public Continuum(final Collection<AddressAndClient> clients) {
this(clients, VNODE_RATIO);
}

public Continuum(final Collection<AddressAndClient> clients, int vnodeRatio) {
this.ringOfFire = buildRing(clients, vnodeRatio);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

public interface NodeLocator {

RawMemcacheClient findClient(final byte[] key);
RawMemcacheClient findClient(final byte[] key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ResolvingKetamaClient(
final Connector connector,
long shutdownDelay,
TimeUnit shutdownUnit,
Function<Collection<AddressAndClient>, NodeLocator> nodeLocator) {
Function<Collection<AddressAndClient>, NodeLocator> nodeLocator) {
this.resolver = resolver;
this.connector = connector;
this.shutdownDelay = shutdownDelay;
Expand Down Expand Up @@ -220,7 +220,8 @@ private void setPendingClient(final ImmutableList.Builder<RawMemcacheClient> rem

// This may invalidate an existing pendingClient but should be fine since it doesn't have any
// important state of its own.
final KetamaMemcacheClient newClient = new KetamaMemcacheClient(addressAndClients, nodeLocator.apply(addressAndClients));
final KetamaMemcacheClient newClient =
new KetamaMemcacheClient(addressAndClients, nodeLocator.apply(addressAndClients));
this.pendingClient = newClient;

newClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ private void test(
}
}

final KetamaMemcacheClient ketamaMemcacheClient = new KetamaMemcacheClient(clients, new Continuum(clients));
final KetamaMemcacheClient ketamaMemcacheClient =
new KetamaMemcacheClient(clients, new Continuum(clients));
final MemcacheClient<String> memcacheClient = buildClient(ketamaMemcacheClient, binary);

final List<String> requestedKeys = new ArrayList<>();
Expand Down
52 changes: 25 additions & 27 deletions folsom/src/test/java/com/spotify/folsom/ketama/NodeLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableList;
import com.spotify.folsom.RawMemcacheClient;
import com.spotify.folsom.guava.HostAndPort;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import com.google.common.collect.ImmutableList;
import com.spotify.folsom.RawMemcacheClient;
import com.spotify.folsom.guava.HostAndPort;

@RunWith(MockitoJUnitRunner.class)
public class NodeLocatorTest {

Expand Down Expand Up @@ -192,32 +190,32 @@ public void testWrapDisconnected() {
public void testPrefixNodeLocator() {
final List<AddressAndClient> clients = ImmutableList.of(AAC1, AAC2, AAC3);
final Continuum c = new Continuum(clients);
final NodeLocator nodeLocator = key -> {
String[] keyParts = new String(key, StandardCharsets.US_ASCII).split("-");
if (keyParts.length > 1) {
return clients.get(Integer.parseInt(keyParts[0])-1).getClient();
} else {
return c.findClient(key);
}
};

final NodeLocator nodeLocator =
key -> {
String[] keyParts = new String(key, StandardCharsets.US_ASCII).split("-");
if (keyParts.length > 1) {
return clients.get(Integer.parseInt(keyParts[0]) - 1).getClient();
} else {
return c.findClient(key);
}
};

List<RawMemcacheClient> actual =
Arrays.asList(
nodeLocator.findClient(bytes("1-key1")),
nodeLocator.findClient(bytes("1-key2")),
nodeLocator.findClient(bytes("1-key3")),
nodeLocator.findClient(bytes("2-key1")),
nodeLocator.findClient(bytes("3-key1")),
nodeLocator.findClient(bytes(KEY1)),
nodeLocator.findClient(bytes(KEY3)));
Arrays.asList(
nodeLocator.findClient(bytes("1-key1")),
nodeLocator.findClient(bytes("1-key2")),
nodeLocator.findClient(bytes("1-key3")),
nodeLocator.findClient(bytes("2-key1")),
nodeLocator.findClient(bytes("3-key1")),
nodeLocator.findClient(bytes(KEY1)),
nodeLocator.findClient(bytes(KEY3)));

List<RawMemcacheClient> expected =
Arrays.asList(
CLIENT1, CLIENT1, CLIENT1, // keys prefixed with 1
CLIENT2, // keys prefixed with 2
CLIENT3, // keys prefixed with 3
CLIENT1, CLIENT2); // fallback to default NodeLocator
Arrays.asList(
CLIENT1, CLIENT1, CLIENT1, // keys prefixed with 1
CLIENT2, // keys prefixed with 2
CLIENT3, // keys prefixed with 3
CLIENT1, CLIENT2); // fallback to default NodeLocator
assertEquals(expected, actual);
}

Expand Down

0 comments on commit 10b8af2

Please sign in to comment.