use of org.apache.beam.sdk.transforms.windowing.GlobalWindow in project beam by apache.
the class ExecutableStageDoFnOperatorTest method testCacheTokenHandling.
@Test
public void testCacheTokenHandling() throws Exception {
InMemoryStateInternals test = InMemoryStateInternals.forKey("test");
KeyedStateBackend<ByteBuffer> stateBackend = FlinkStateInternalsTest.createStateBackend();
ExecutableStageDoFnOperator.BagUserStateFactory<Integer, GlobalWindow> bagUserStateFactory = new ExecutableStageDoFnOperator.BagUserStateFactory<>(test, stateBackend, NoopLock.get(), null);
ByteString key1 = ByteString.copyFrom("key1", Charsets.UTF_8);
ByteString key2 = ByteString.copyFrom("key2", Charsets.UTF_8);
Map<String, Map<String, ProcessBundleDescriptors.BagUserStateSpec>> userStateMapMock = Mockito.mock(Map.class);
Map<String, ProcessBundleDescriptors.BagUserStateSpec> transformMap = Mockito.mock(Map.class);
final String userState1 = "userstate1";
ProcessBundleDescriptors.BagUserStateSpec bagUserStateSpec1 = mockBagUserState(userState1);
when(transformMap.get(userState1)).thenReturn(bagUserStateSpec1);
final String userState2 = "userstate2";
ProcessBundleDescriptors.BagUserStateSpec bagUserStateSpec2 = mockBagUserState(userState2);
when(transformMap.get(userState2)).thenReturn(bagUserStateSpec2);
when(userStateMapMock.get(anyString())).thenReturn(transformMap);
when(processBundleDescriptor.getBagUserStateSpecs()).thenReturn(userStateMapMock);
StateRequestHandler stateRequestHandler = StateRequestHandlers.forBagUserStateHandlerFactory(processBundleDescriptor, bagUserStateFactory);
// User state the cache token is valid for the lifetime of the operator
final BeamFnApi.ProcessBundleRequest.CacheToken expectedCacheToken = Iterables.getOnlyElement(stateRequestHandler.getCacheTokens());
// Make a request to generate initial cache token
stateRequestHandler.handle(getRequest(key1, userState1));
BeamFnApi.ProcessBundleRequest.CacheToken returnedCacheToken = Iterables.getOnlyElement(stateRequestHandler.getCacheTokens());
assertThat(returnedCacheToken.hasUserState(), is(true));
assertThat(returnedCacheToken, is(expectedCacheToken));
List<RequestGenerator> generators = Arrays.asList(ExecutableStageDoFnOperatorTest::getRequest, ExecutableStageDoFnOperatorTest::getAppend, ExecutableStageDoFnOperatorTest::getClear);
for (RequestGenerator req : generators) {
// For every state read the tokens remains unchanged
stateRequestHandler.handle(req.makeRequest(key1, userState1));
assertThat(Iterables.getOnlyElement(stateRequestHandler.getCacheTokens()), is(expectedCacheToken));
// The token is still valid for another key in the same key range
stateRequestHandler.handle(req.makeRequest(key2, userState1));
assertThat(Iterables.getOnlyElement(stateRequestHandler.getCacheTokens()), is(expectedCacheToken));
// The token is still valid for another state cell in the same key range
stateRequestHandler.handle(req.makeRequest(key2, userState2));
assertThat(Iterables.getOnlyElement(stateRequestHandler.getCacheTokens()), is(expectedCacheToken));
}
}
use of org.apache.beam.sdk.transforms.windowing.GlobalWindow in project beam by apache.
the class DataflowProcessFnRunner method checkTrivialOuterWindows.
// TODO: move this and the next function into ProcessFn.
private static <T> void checkTrivialOuterWindows(WindowedValue<KeyedWorkItem<byte[], T>> windowedKWI) {
// In practice it will be in 0 or 1 windows (ValueInEmptyWindows or ValueInGlobalWindow)
Collection<? extends BoundedWindow> outerWindows = windowedKWI.getWindows();
if (!outerWindows.isEmpty()) {
checkArgument(outerWindows.size() == 1, "The KeyedWorkItem itself must not be in multiple windows, but was in: %s", outerWindows);
BoundedWindow onlyWindow = Iterables.getOnlyElement(outerWindows);
checkArgument(onlyWindow instanceof GlobalWindow, "KeyedWorkItem must be in the Global window, but was in: %s", onlyWindow);
}
}
Aggregations