Make listeners a final field in ShardIndexingService, which fixed possible visibility issue.

This commit is contained in:
Martijn van Groningen 2013-12-11 12:22:45 +01:00
parent aee388ec46
commit a760f1f54a
1 changed files with 44 additions and 75 deletions

View File

@ -44,9 +44,9 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
private final StatsHolder totalStats = new StatsHolder();
private volatile Map<String, StatsHolder> typesStats = ImmutableMap.of();
private final CopyOnWriteArrayList<IndexingOperationListener> listeners = new CopyOnWriteArrayList<IndexingOperationListener>();
private CopyOnWriteArrayList<IndexingOperationListener> listeners = null;
private volatile Map<String, StatsHolder> typesStats = ImmutableMap.of();
@Inject
public ShardIndexingService(ShardId shardId, @IndexSettings Settings indexSettings, ShardSlowLogIndexingService slowLog) {
@ -81,36 +81,24 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
return new IndexingStats(total, typesSt);
}
public synchronized void addListener(IndexingOperationListener listener) {
if (listeners == null) {
listeners = new CopyOnWriteArrayList<IndexingOperationListener>();
}
public void addListener(IndexingOperationListener listener) {
listeners.add(listener);
}
public synchronized void removeListener(IndexingOperationListener listener) {
if (listeners == null) {
return;
}
public void removeListener(IndexingOperationListener listener) {
listeners.remove(listener);
if (listeners.isEmpty()) {
listeners = null;
}
}
public Engine.Create preCreate(Engine.Create create) {
totalStats.indexCurrent.inc();
typeStats(create.type()).indexCurrent.inc();
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
create = listener.preCreate(create);
}
}
return create;
}
public void postCreateUnderLock(Engine.Create create) {
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
try {
listener.postCreateUnderLock(create);
@ -119,7 +107,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
}
}
}
public void postCreate(Engine.Create create) {
long took = create.endTime() - create.startTime();
@ -129,7 +116,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
typeStats.indexMetric.inc(took);
typeStats.indexCurrent.dec();
slowLog.postCreate(create, took);
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
try {
listener.postCreate(create);
@ -138,21 +124,17 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
}
}
}
public Engine.Index preIndex(Engine.Index index) {
totalStats.indexCurrent.inc();
typeStats(index.type()).indexCurrent.inc();
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
index = listener.preIndex(index);
}
}
return index;
}
public void postIndexUnderLock(Engine.Index index) {
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
try {
listener.postIndexUnderLock(index);
@ -161,7 +143,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
}
}
}
public void postIndex(Engine.Index index) {
long took = index.endTime() - index.startTime();
@ -171,7 +152,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
typeStats.indexMetric.inc(took);
typeStats.indexCurrent.dec();
slowLog.postIndex(index, took);
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
try {
listener.postIndex(index);
@ -180,7 +160,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
}
}
}
public void failedIndex(Engine.Index index) {
totalStats.indexCurrent.dec();
@ -190,16 +169,13 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
public Engine.Delete preDelete(Engine.Delete delete) {
totalStats.deleteCurrent.inc();
typeStats(delete.type()).deleteCurrent.inc();
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
delete = listener.preDelete(delete);
}
}
return delete;
}
public void postDeleteUnderLock(Engine.Delete delete) {
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
try {
listener.postDeleteUnderLock(delete);
@ -208,7 +184,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
}
}
}
public void postDelete(Engine.Delete delete) {
long took = delete.endTime() - delete.startTime();
@ -217,7 +192,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
StatsHolder typeStats = typeStats(delete.type());
typeStats.deleteMetric.inc(took);
typeStats.deleteCurrent.dec();
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
try {
listener.postDelete(delete);
@ -226,7 +200,6 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
}
}
}
public void failedDelete(Engine.Delete delete) {
totalStats.deleteCurrent.dec();
@ -234,21 +207,17 @@ public class ShardIndexingService extends AbstractIndexShardComponent {
}
public Engine.DeleteByQuery preDeleteByQuery(Engine.DeleteByQuery deleteByQuery) {
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
deleteByQuery = listener.preDeleteByQuery(deleteByQuery);
}
}
return deleteByQuery;
}
public void postDeleteByQuery(Engine.DeleteByQuery deleteByQuery) {
if (listeners != null) {
for (IndexingOperationListener listener : listeners) {
listener.postDeleteByQuery(deleteByQuery);
}
}
}
public void clear() {
totalStats.clear();