Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

[20 min task] client -> namespace #2654

Merged
merged 1 commit into from
Nov 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
import com.palantir.timestamp.TimestampManagementService;
import com.palantir.timestamp.TimestampService;

@Path("/{client: [a-zA-Z0-9_-]+}")
@Path("/{namespace: [a-zA-Z0-9_-]+}")
public class TimeLockResource {
private final Logger log = LoggerFactory.getLogger(TimeLockResource.class);

private final Function<String, TimeLockServices> clientServicesFactory;
private final ConcurrentMap<String, TimeLockServices> servicesByClient = Maps.newConcurrentMap();
private final ConcurrentMap<String, TimeLockServices> servicesByNamespace = Maps.newConcurrentMap();
private final Supplier<Integer> maxNumberOfClients;

public TimeLockResource(
Expand All @@ -51,46 +51,46 @@ public TimeLockResource(
}

@Path("/lock")
public LockService getLockService(@Safe @PathParam("client") String client) {
return getOrCreateServices(client).getLockService();
public LockService getLockService(@Safe @PathParam("namespace") String namespace) {
return getOrCreateServices(namespace).getLockService();
}

@Path("/timestamp")
public TimestampService getTimeService(@Safe @PathParam("client") String client) {
return getOrCreateServices(client).getTimestampService();
public TimestampService getTimeService(@Safe @PathParam("namespace") String namespace) {
return getOrCreateServices(namespace).getTimestampService();
}

@Path("/timelock")
public Object getTimelockService(@Safe @PathParam("client") String client) {
return getOrCreateServices(client).getTimelockService().getPresentService();
public Object getTimelockService(@Safe @PathParam("namespace") String namespace) {
return getOrCreateServices(namespace).getTimelockService().getPresentService();
}

@Path("/timestamp-management")
public TimestampManagementService getTimestampManagementService(@Safe @PathParam("client") String client) {
return getOrCreateServices(client).getTimestampManagementService();
public TimestampManagementService getTimestampManagementService(@Safe @PathParam("namespace") String namespace) {
return getOrCreateServices(namespace).getTimestampManagementService();
}

@VisibleForTesting
TimeLockServices getOrCreateServices(String client) {
return servicesByClient.computeIfAbsent(client, this::createNewClient);
TimeLockServices getOrCreateServices(String namespace) {
return servicesByNamespace.computeIfAbsent(namespace, this::createNewClient);
}

private TimeLockServices createNewClient(String client) {
Preconditions.checkArgument(!client.equals(PaxosTimeLockConstants.LEADER_ELECTION_NAMESPACE),
private TimeLockServices createNewClient(String namespace) {
Preconditions.checkArgument(!namespace.equals(PaxosTimeLockConstants.LEADER_ELECTION_NAMESPACE),
"The client name '%s' is reserved for the leader election service, and may not be "
+ "used.",
PaxosTimeLockConstants.LEADER_ELECTION_NAMESPACE);

if (servicesByClient.size() >= maxNumberOfClients.get()) {
if (servicesByNamespace.size() >= maxNumberOfClients.get()) {
log.error(
"Unable to create timelock services for client {}, as it would exceed the maximum number of "
+ "allowed clients ({}). If this is intentional, the maximum number of clients can be "
+ "increased via the maximum-number-of-clients runtime config property.",
SafeArg.of("client", client),
SafeArg.of("client", namespace),
SafeArg.of("maxNumberOfClients", maxNumberOfClients));
throw new IllegalStateException("Maximum number of clients exceeded");
}

return clientServicesFactory.apply(client);
return clientServicesFactory.apply(namespace);
}
}