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

Commit

Permalink
Merge pull request #985 from zalando/sub-cache
Browse files Browse the repository at this point in the history
Instance based subscription cache for cursor service
  • Loading branch information
adyach authored Dec 4, 2018
2 parents 4e72b63 + 41a76b5 commit 62bc7ea
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.zalando.nakadi.service.AuthorizationValidator;
import org.zalando.nakadi.service.CursorConverter;
import org.zalando.nakadi.service.CursorsService;
import org.zalando.nakadi.service.SubscriptionCache;
import org.zalando.nakadi.service.subscription.model.Partition;
import org.zalando.nakadi.service.subscription.zk.NewZkSubscriptionClient;
import org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory;
Expand Down Expand Up @@ -105,12 +106,13 @@ public void before() throws Exception {
when(subscription.getEventTypes()).thenReturn(ImmutableSet.of(etName));
final SubscriptionDbRepository subscriptionRepo = mock(SubscriptionDbRepository.class);
when(subscriptionRepo.getSubscription(sid)).thenReturn(subscription);
final SubscriptionCache subscriptionCache = mock(SubscriptionCache.class);
when(subscriptionCache.getSubscription(sid)).thenReturn(subscription);
final SubscriptionClientFactory zkSubscriptionFactory = new SubscriptionClientFactory(zkHolder, MAPPER);
uuidGenerator = mock(UUIDGenerator.class);
when(uuidGenerator.isUUID(any())).thenReturn(true);
cursorsService = new CursorsService(subscriptionRepo, null,
mock(NakadiSettings.class), zkSubscriptionFactory, cursorConverter, uuidGenerator, null,
mock(AuthorizationValidator.class));
cursorsService = new CursorsService(subscriptionRepo, subscriptionCache, null, mock(NakadiSettings.class),
zkSubscriptionFactory, cursorConverter, uuidGenerator, null, mock(AuthorizationValidator.class));

// Register cursors in converter
registerNakadiCursor(NakadiCursor.of(buildTimeline(etName, topic, CREATED_AT), P1, NEW_OFFSET));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;
import org.zalando.nakadi.domain.ItemsWrapper;
import org.zalando.nakadi.domain.NakadiCursor;
import org.zalando.nakadi.exceptions.runtime.CursorsAreEmptyException;
Expand Down Expand Up @@ -71,8 +70,7 @@ public ItemsWrapper<SubscriptionCursor> getCursors(@PathVariable("subscriptionId
@RequestMapping(value = "/subscriptions/{subscriptionId}/cursors", method = RequestMethod.POST)
public ResponseEntity<?> commitCursors(@PathVariable("subscriptionId") final String subscriptionId,
@Valid @RequestBody final ItemsWrapper<SubscriptionCursor> cursorsIn,
@NotNull @RequestHeader("X-Nakadi-StreamId") final String streamId,
final NativeWebRequest request)
@NotNull @RequestHeader("X-Nakadi-StreamId") final String streamId)
throws NoSuchEventTypeException,
NoSuchSubscriptionException,
InvalidCursorException,
Expand All @@ -98,8 +96,7 @@ public ResponseEntity<?> commitCursors(@PathVariable("subscriptionId") final Str
@RequestMapping(value = "/subscriptions/{subscriptionId}/cursors", method = RequestMethod.PATCH)
public ResponseEntity<?> resetCursors(
@PathVariable("subscriptionId") final String subscriptionId,
@Valid @RequestBody final ItemsWrapper<SubscriptionCursorWithoutToken> cursors,
final NativeWebRequest request)
@Valid @RequestBody final ItemsWrapper<SubscriptionCursorWithoutToken> cursors)
throws NoSuchEventTypeException, InvalidCursorException, InternalNakadiException {
cursorsService.resetCursors(subscriptionId, convertToNakadiCursors(cursors));
return noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public NoSuchSubscriptionException(final String message) {
public NoSuchSubscriptionException(final String msg, final Exception cause) {
super(msg, cause);
}

public static NoSuchSubscriptionException withSubscriptionId(final String subscriptionId, final Exception cause) {
return new NoSuchSubscriptionException("Subscription with id \"" + subscriptionId + "\" does not exist", cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Subscription getSubscription(final String id) throws NoSuchSubscriptionEx
try {
return jdbcTemplate.queryForObject(sql, new Object[]{id}, rowMapper);
} catch (final EmptyResultDataAccessException e) {
throw new NoSuchSubscriptionException("Subscription with id \"" + id + "\" does not exist");
throw NoSuchSubscriptionException.withSubscriptionId(id, e);
} catch (final DataAccessException e) {
LOG.error("Database error when getting subscription", e);
throw new ServiceTemporarilyUnavailableException("Error occurred when running database request");
Expand All @@ -113,7 +113,7 @@ public void deleteSubscription(final String id)
try {
final int rowsDeleted = jdbcTemplate.update("DELETE FROM zn_data.subscription WHERE s_id = ?", id);
if (rowsDeleted == 0) {
throw new NoSuchSubscriptionException("Subscription with id \"" + id + "\" does not exist");
throw NoSuchSubscriptionException.withSubscriptionId(id, null);
}
} catch (final DataAccessException e) {
LOG.error("Database error when deleting subscription", e);
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/org/zalando/nakadi/service/CursorsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import org.zalando.nakadi.domain.EventTypePartition;
import org.zalando.nakadi.domain.NakadiCursor;
import org.zalando.nakadi.domain.Subscription;
import org.zalando.nakadi.exceptions.runtime.NakadiRuntimeException;
import org.zalando.nakadi.exceptions.runtime.AccessDeniedException;
import org.zalando.nakadi.exceptions.runtime.InternalNakadiException;
import org.zalando.nakadi.exceptions.runtime.InvalidCursorException;
import org.zalando.nakadi.exceptions.runtime.InvalidStreamIdException;
import org.zalando.nakadi.exceptions.runtime.NakadiRuntimeException;
import org.zalando.nakadi.exceptions.runtime.NoSuchEventTypeException;
import org.zalando.nakadi.exceptions.runtime.NoSuchSubscriptionException;
import org.zalando.nakadi.exceptions.runtime.OperationTimeoutException;
Expand Down Expand Up @@ -43,32 +43,36 @@
@Component
public class CursorsService {

private final SubscriptionDbRepository subscriptionRepository;

private final EventTypeCache eventTypeCache;
private final NakadiSettings nakadiSettings;
private final SubscriptionClientFactory zkSubscriptionFactory;
private final CursorConverter cursorConverter;
private final UUIDGenerator uuidGenerator;
private final TimelineService timelineService;
private final AuthorizationValidator authorizationValidator;
private final SubscriptionDbRepository subscriptionRepository;
private final SubscriptionCache subscriptionCache;

@Autowired
public CursorsService(final SubscriptionDbRepository subscriptionRepository,
final SubscriptionCache subscriptionCache,
final EventTypeCache eventTypeCache,
final NakadiSettings nakadiSettings,
final SubscriptionClientFactory zkSubscriptionFactory,
final CursorConverter cursorConverter,
final UUIDGenerator uuidGenerator,
final TimelineService timelineService,
final AuthorizationValidator authorizationValidator) {
this.subscriptionRepository = subscriptionRepository;
this.eventTypeCache = eventTypeCache;
this.nakadiSettings = nakadiSettings;
this.zkSubscriptionFactory = zkSubscriptionFactory;
this.cursorConverter = cursorConverter;
this.uuidGenerator = uuidGenerator;
this.timelineService = timelineService;
this.authorizationValidator = authorizationValidator;
this.subscriptionRepository = subscriptionRepository;
this.subscriptionCache = subscriptionCache;
}

/**
Expand All @@ -79,7 +83,7 @@ public List<Boolean> commitCursors(final String streamId, final String subscript
throws ServiceTemporarilyUnavailableException, InvalidCursorException, InvalidStreamIdException,
NoSuchEventTypeException, InternalNakadiException, NoSuchSubscriptionException, UnableProcessException,
AccessDeniedException {
final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
final Subscription subscription = subscriptionCache.getSubscription(subscriptionId);

authorizationValidator.authorizeSubscriptionCommit(subscription);

Expand All @@ -88,23 +92,29 @@ public List<Boolean> commitCursors(final String streamId, final String subscript
final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(
subscription, LogPathBuilder.build(subscriptionId, streamId, "offsets"));

validateStreamId(cursors, streamId, zkClient);
validateStreamId(cursors, streamId, zkClient, subscriptionId);

return zkClient.commitOffsets(
cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()),
new SubscriptionCursorComparator(new NakadiCursorComparator(eventTypeCache)));
}

private void validateStreamId(final List<NakadiCursor> cursors, final String streamId,
final ZkSubscriptionClient subscriptionClient)
throws ServiceTemporarilyUnavailableException, InvalidCursorException, InvalidStreamIdException {
private void validateStreamId(final List<NakadiCursor> cursors,
final String streamId,
final ZkSubscriptionClient subscriptionClient,
final String subscriptionId)
throws ServiceTemporarilyUnavailableException,
InvalidCursorException,
InvalidStreamIdException,
SubscriptionNotInitializedException {

if (!uuidGenerator.isUUID(streamId)) {
throw new InvalidStreamIdException(
String.format("Stream id has to be valid UUID, but `%s was provided", streamId), streamId);
}

if (!subscriptionClient.isActiveSession(streamId)) {
subscriptionCache.invalidateSubscription(subscriptionId);
throw new InvalidStreamIdException("Session with stream id " + streamId + " not found", streamId);
}

Expand Down
69 changes: 69 additions & 0 deletions src/main/java/org/zalando/nakadi/service/SubscriptionCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.zalando.nakadi.service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.zalando.nakadi.domain.Subscription;
import org.zalando.nakadi.exceptions.runtime.NoSuchSubscriptionException;
import org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException;
import org.zalando.nakadi.repository.db.SubscriptionDbRepository;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
* The caching works only inside the instance of the application, it does not sync cache across the instances, that's
* why one should be careful about using it.
*/
@Service
public class SubscriptionCache {

private final LoadingCache<String, Subscription> subscriptionsCache;

@Autowired
public SubscriptionCache(
final SubscriptionDbRepository subscriptionRepository,
@Value("${nakadi.cache.subscription.expireAfterAccessMs:300000}") final long expireAfterAccessMs) {
this.subscriptionsCache = CacheBuilder.newBuilder()
.expireAfterAccess(expireAfterAccessMs, TimeUnit.MILLISECONDS)
.build(new CacheLoader<String, Subscription>() {
@Override
public Subscription load(final String subscriptionId)
throws NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
return subscriptionRepository.getSubscription(subscriptionId);
}
});
}

/**
* The method is not synced across Nakadi instances and eventually consistent in case of invalidation on
* different instance, it may return stale data.
*
* @param subscriptionId
* @return cached subscription or exception
* @throws NoSuchSubscriptionException
* @throws ServiceTemporarilyUnavailableException
*/
public Subscription getSubscription(final String subscriptionId)
throws NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
try {
return subscriptionsCache.get(subscriptionId);
} catch (final UncheckedExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof NoSuchSubscriptionException) {
throw (NoSuchSubscriptionException) cause;
}
throw new ServiceTemporarilyUnavailableException("Failed to access subscription cache", cause);
} catch (final ExecutionException e) {
throw new ServiceTemporarilyUnavailableException("Failed to access subscription cache", e.getCause());
}
}

public void invalidateSubscription(final String subscriptionId) {
subscriptionsCache.invalidate(subscriptionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.zalando.nakadi.exceptions.runtime.SubscriptionUpdateConflictException;
import org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException;
import org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException;

import org.zalando.nakadi.repository.EventTypeRepository;
import org.zalando.nakadi.repository.TopicRepository;
import org.zalando.nakadi.repository.db.SubscriptionDbRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public final Collection<Session> listSessions()
for (int i = 0; i < 5; i++) {
try {
final List<String> sessions = getCurator().getChildren().forPath(getSubscriptionPath("/sessions"));
final Map <String,Session> result = loadDataAsync(sessions,
final Map<String, Session> result = loadDataAsync(sessions,
key -> getSubscriptionPath("/sessions/" + key),
this::deserializeSession);
if (result.size() == sessions.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class CursorsServiceTest {
@Before
public void setup() {
authorizationValidator = mock(AuthorizationValidator.class);
service = new CursorsService(
mock(SubscriptionDbRepository.class), null, null, null, null, null, null, authorizationValidator);
service = new CursorsService(mock(SubscriptionDbRepository.class), mock(SubscriptionCache.class), null, null,
null, null, null, null, authorizationValidator);
}

@Test(expected = AccessDeniedException.class)
Expand All @@ -38,4 +38,4 @@ public void whenCommitCursorsAccessDenied() throws Exception {
.when(authorizationValidator).authorizeSubscriptionCommit(any());
service.commitCursors("test", "test", Collections.emptyList());
}
}
}

0 comments on commit 62bc7ea

Please sign in to comment.