Skip to content

Commit

Permalink
GEODE-2674: Changing the lucene listener to fetch the value from the …
Browse files Browse the repository at this point in the history
…region

Rather than use the value that is in the queue, use the latest value
from the region to update the lucene index.

This ensures that even if the queue contains spurious events due to
retries or other issues, we put the correct value in the index.

This also potentially saves memory and disk space for the queue, because
the queue does not need to hold the value for the entry.
  • Loading branch information
upthewaterspout committed Mar 16, 2017
1 parent f329f4a commit 1602a26
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.List;
import java.util.Set;

import org.apache.geode.cache.EntryDestroyedException;
import org.apache.geode.cache.Region.Entry;
import org.apache.geode.internal.cache.wan.parallel.ParallelGatewaySenderQueue;
import org.apache.logging.log4j.Logger;
import org.apache.geode.cache.CacheClosedException;
Expand Down Expand Up @@ -87,19 +89,20 @@ protected boolean process(final List<AsyncEvent> events) {

IndexRepository repository = repositoryManager.getRepository(region, key, callbackArgument);

Operation op = event.getOperation();
final Entry entry = region.getEntry(key);
Object value;
try {
value = entry == null ? null : entry.getValue();
} catch (EntryDestroyedException e) {
value = null;
}

if (op.isCreate()) {
repository.update(key, event.getDeserializedValue());
} else if (op.isUpdate()) {
repository.update(key, event.getDeserializedValue());
} else if (op.isDestroy()) {
repository.delete(key);
} else if (op.isInvalidate()) {
repository.delete(key);
if (value != null) {
repository.update(key, value);
} else {
throw new InternalGemFireError("Unhandled operation " + op + " on " + event.getRegion());
repository.delete(key);
}

affectedRepos.add(repository);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package org.apache.geode.cache.lucene.internal;

import org.apache.geode.cache.EntryEvent;
import org.apache.geode.cache.wan.GatewayEventSubstitutionFilter;
import org.apache.geode.internal.cache.Token;

/**
* A substitution filter which throws away the value of the entry and replaces it with an empty
* string. For the lucene index, we will just fetch the new value from the region, so we don't need
* the value.
*/
public class LuceneEventSubstitutionFilter implements GatewayEventSubstitutionFilter {
@Override
public Object getSubstituteValue(final EntryEvent event) {
return "";
}

@Override
public void close() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ private AsyncEventQueue createAEQ(AsyncEventQueueFactoryImpl factory) {
return null;
}
LuceneEventListener listener = new LuceneEventListener(repositoryManager);
factory.setGatewayEventSubstitutionListener(new LuceneEventSubstitutionFilter());
String aeqId = LuceneServiceImpl.getUniqueIndexName(getName(), regionPath);
AsyncEventQueue indexQueue = factory.create(aeqId, listener);
return indexQueue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.concurrent.atomic.AtomicReference;

import org.apache.geode.InternalGemFireError;
import org.apache.geode.cache.Region.Entry;
import org.apache.geode.internal.cache.RegionEntry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -80,18 +82,16 @@ public void testProcessBatch() throws Exception {
Mockito.when(event.getKey()).thenReturn(i);
Mockito.when(event.getCallbackArgument()).thenReturn(callback);

switch (i % 3) {
switch (i % 4) {
case 0:
Mockito.when(event.getOperation()).thenReturn(Operation.CREATE);
Mockito.when(event.getDeserializedValue()).thenReturn(i);
break;
case 1:
Mockito.when(event.getOperation()).thenReturn(Operation.UPDATE);
Mockito.when(event.getDeserializedValue()).thenReturn(i);
final Entry entry = mock(Entry.class);
when(entry.getValue()).thenReturn(i);
when(region.getEntry(eq(i))).thenReturn(entry);
break;
case 2:
Mockito.when(event.getOperation()).thenReturn(Operation.DESTROY);
Mockito.when(event.getDeserializedValue()).thenThrow(new AssertionError());
case 3:
// Do nothing, get value will return a destroy
break;
}

Expand All @@ -100,10 +100,10 @@ public void testProcessBatch() throws Exception {

listener.processEvents(events);

verify(repo1, atLeast(numEntries / 6)).delete(any());
verify(repo1, atLeast(numEntries / 3)).update(any(), any());
verify(repo2, atLeast(numEntries / 6)).delete(any());
verify(repo2, atLeast(numEntries / 3)).update(any(), any());
verify(repo1, atLeast(numEntries / 4)).delete(any());
verify(repo1, atLeast(numEntries / 4)).update(any(), any());
verify(repo2, atLeast(numEntries / 4)).delete(any());
verify(repo2, atLeast(numEntries / 4)).update(any(), any());
verify(repo1, times(1)).commit();
verify(repo2, times(1)).commit();
}
Expand Down

0 comments on commit 1602a26

Please sign in to comment.