Skip to content

Commit

Permalink
[BWC and API enforcement] Reduce the visibility of some existing APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed Nov 16, 2023
1 parent dc5a299 commit bd4ae8d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.action.OriginalIndices;
import org.opensearch.common.Nullable;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.common.annotation.InternalApi;
import org.opensearch.common.lease.Releasable;
import org.opensearch.common.util.concurrent.AtomicArray;
import org.opensearch.search.SearchPhaseResult;
Expand All @@ -49,9 +49,9 @@
/**
* This class provide contextual state and access to resources across multiple search phases.
*
* @opensearch.api
* @opensearch.internal
*/
@PublicApi(since = "1.0.0")
@InternalApi
public interface SearchPhaseContext extends Executor {
// TODO maybe we can make this concrete later - for now we just implement this in the base class for all initial phases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.opensearch.common.annotation.PublicApi;
import org.opensearch.common.annotation.InternalApi;

import java.util.List;

/**
* A listener for search, fetch and context events at the coordinator node level
*
* @opensearch.api
* @opensearch.internal
*/
@PublicApi(since = "1.0.0")
public interface SearchRequestOperationsListener {
@InternalApi
interface SearchRequestOperationsListener {

void onPhaseStart(SearchPhaseContext context);
void onPhaseStart(SearchPhase phase);

void onPhaseEnd(SearchPhaseContext context);
void onPhaseEnd(SearchPhase phase);

void onPhaseFailure(SearchPhaseContext context);
void onPhaseFailure(SearchPhase phase);

/**
* Holder of Composite Listeners
Expand All @@ -44,32 +44,32 @@ public CompositeListener(List<SearchRequestOperationsListener> listeners, Logger
}

@Override
public void onPhaseStart(SearchPhaseContext context) {
public void onPhaseStart(SearchPhase phase) {
for (SearchRequestOperationsListener listener : listeners) {
try {
listener.onPhaseStart(context);
listener.onPhaseStart(phase);
} catch (Exception e) {
logger.warn(() -> new ParameterizedMessage("onPhaseStart listener [{}] failed", listener), e);
}
}
}

@Override
public void onPhaseEnd(SearchPhaseContext context) {
public void onPhaseEnd(SearchPhase phase) {
for (SearchRequestOperationsListener listener : listeners) {
try {
listener.onPhaseEnd(context);
listener.onPhaseEnd(phase);
} catch (Exception e) {
logger.warn(() -> new ParameterizedMessage("onPhaseEnd listener [{}] failed", listener), e);
}
}
}

@Override
public void onPhaseFailure(SearchPhaseContext context) {
public void onPhaseFailure(SearchPhase phase) {
for (SearchRequestOperationsListener listener : listeners) {
try {
listener.onPhaseFailure(context);
listener.onPhaseFailure(phase);
} catch (Exception e) {
logger.warn(() -> new ParameterizedMessage("onPhaseFailure listener [{}] failed", listener), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ public long getPhaseMetric(SearchPhaseName searchPhaseName) {
}

@Override
public void onPhaseStart(SearchPhaseContext context) {
phaseStatsMap.get(context.getCurrentPhase().getSearchPhaseName()).current.inc();
public void onPhaseStart(SearchPhase phase) {
phaseStatsMap.get(phase.getSearchPhaseName()).current.inc();
}

@Override
public void onPhaseEnd(SearchPhaseContext context) {
StatsHolder phaseStats = phaseStatsMap.get(context.getCurrentPhase().getSearchPhaseName());
public void onPhaseEnd(SearchPhase phase) {
StatsHolder phaseStats = phaseStatsMap.get(phase.getSearchPhaseName());
phaseStats.current.dec();
phaseStats.total.inc();
phaseStats.timing.inc(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - context.getCurrentPhase().getStartTimeInNanos()));
phaseStats.timing.inc(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - phase.getStartTimeInNanos()));
}

@Override
public void onPhaseFailure(SearchPhaseContext context) {
phaseStatsMap.get(context.getCurrentPhase().getSearchPhaseName()).current.dec();
public void onPhaseFailure(SearchPhase phase) {
phaseStatsMap.get(phase.getSearchPhaseName()).current.dec();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,15 @@ SearchResponse.PhaseTook getPhaseTook() {
Map<SearchPhaseName, Long> phaseStatsMap = new EnumMap<>(SearchPhaseName.class);

@Override
public void onPhaseStart(SearchPhaseContext context) {}
public void onPhaseStart(SearchPhase phase) {}

@Override
public void onPhaseEnd(SearchPhaseContext context) {
phaseStatsMap.put(
context.getCurrentPhase().getSearchPhaseName(),
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - context.getCurrentPhase().getStartTimeInNanos())
);
public void onPhaseEnd(SearchPhase phase) {
phaseStatsMap.put(phase.getSearchPhaseName(), TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - phase.getStartTimeInNanos()));
}

@Override
public void onPhaseFailure(SearchPhaseContext context) {}
public void onPhaseFailure(SearchPhase phase) {}

public Long getPhaseTookTime(SearchPhaseName searchPhaseName) {
return phaseStatsMap.get(searchPhaseName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ public void testListenersAreExecuted() {
SearchRequestOperationsListener testListener = new SearchRequestOperationsListener() {

@Override
public void onPhaseStart(SearchPhaseContext context) {
searchPhaseMap.get(context.getCurrentPhase().getSearchPhaseName()).current.inc();
public void onPhaseStart(SearchPhase phase) {
searchPhaseMap.get(phase.getSearchPhaseName()).current.inc();
}

@Override
public void onPhaseEnd(SearchPhaseContext context) {
searchPhaseMap.get(context.getCurrentPhase().getSearchPhaseName()).current.dec();
searchPhaseMap.get(context.getCurrentPhase().getSearchPhaseName()).total.inc();
public void onPhaseEnd(SearchPhase phase) {
searchPhaseMap.get(phase.getSearchPhaseName()).current.dec();
searchPhaseMap.get(phase.getSearchPhaseName()).total.inc();
}

@Override
public void onPhaseFailure(SearchPhaseContext context) {
searchPhaseMap.get(context.getCurrentPhase().getSearchPhaseName()).current.dec();
public void onPhaseFailure(SearchPhase phase) {
searchPhaseMap.get(phase.getSearchPhaseName()).current.dec();
}
};

Expand All @@ -62,7 +62,7 @@ public void onPhaseFailure(SearchPhaseContext context) {
for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) {
when(ctx.getCurrentPhase()).thenReturn(searchPhase);
when(searchPhase.getSearchPhaseName()).thenReturn(searchPhaseName);
compositeListener.onPhaseStart(ctx);
compositeListener.onPhaseStart(ctx.getCurrentPhase());
assertEquals(totalListeners, searchPhaseMap.get(searchPhaseName).current.count());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void testSearchRequestPhaseFailure() {

for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) {
when(mockSearchPhase.getSearchPhaseName()).thenReturn(searchPhaseName);
testRequestStats.onPhaseStart(ctx);
testRequestStats.onPhaseStart(ctx.getCurrentPhase());
assertEquals(1, testRequestStats.getPhaseCurrent(searchPhaseName));
testRequestStats.onPhaseFailure(ctx);
testRequestStats.onPhaseFailure(ctx.getCurrentPhase());
assertEquals(0, testRequestStats.getPhaseCurrent(searchPhaseName));
}
}
Expand All @@ -46,11 +46,11 @@ public void testSearchRequestStats() {
for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) {
when(mockSearchPhase.getSearchPhaseName()).thenReturn(searchPhaseName);
long tookTimeInMillis = randomIntBetween(1, 10);
testRequestStats.onPhaseStart(ctx);
testRequestStats.onPhaseStart(ctx.getCurrentPhase());
long startTime = System.nanoTime() - TimeUnit.MILLISECONDS.toNanos(tookTimeInMillis);
when(mockSearchPhase.getStartTimeInNanos()).thenReturn(startTime);
assertEquals(1, testRequestStats.getPhaseCurrent(searchPhaseName));
testRequestStats.onPhaseEnd(ctx);
testRequestStats.onPhaseEnd(ctx.getCurrentPhase());
assertEquals(0, testRequestStats.getPhaseCurrent(searchPhaseName));
assertEquals(1, testRequestStats.getPhaseTotal(searchPhaseName));
assertThat(testRequestStats.getPhaseMetric(searchPhaseName), greaterThanOrEqualTo(tookTimeInMillis));
Expand All @@ -71,7 +71,7 @@ public void testSearchRequestStatsOnPhaseStartConcurrently() throws InterruptedE
for (int i = 0; i < numTasks; i++) {
threads[i] = new Thread(() -> {
phaser.arriveAndAwaitAdvance();
testRequestStats.onPhaseStart(ctx);
testRequestStats.onPhaseStart(ctx.getCurrentPhase());
countDownLatch.countDown();
});
threads[i].start();
Expand Down Expand Up @@ -102,7 +102,7 @@ public void testSearchRequestStatsOnPhaseEndConcurrently() throws InterruptedExc
for (int i = 0; i < numTasks; i++) {
threads[i] = new Thread(() -> {
phaser.arriveAndAwaitAdvance();
testRequestStats.onPhaseEnd(ctx);
testRequestStats.onPhaseEnd(ctx.getCurrentPhase());
countDownLatch.countDown();
});
threads[i].start();
Expand Down Expand Up @@ -134,8 +134,8 @@ public void testSearchRequestStatsOnPhaseFailureConcurrently() throws Interrupte
for (int i = 0; i < numTasks; i++) {
threads[i] = new Thread(() -> {
phaser.arriveAndAwaitAdvance();
testRequestStats.onPhaseStart(ctx);
testRequestStats.onPhaseFailure(ctx);
testRequestStats.onPhaseStart(ctx.getCurrentPhase());
testRequestStats.onPhaseFailure(ctx.getCurrentPhase());
countDownLatch.countDown();
});
threads[i].start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public void testSearchTimeProviderPhaseFailure() {

for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) {
when(mockSearchPhase.getSearchPhaseName()).thenReturn(searchPhaseName);
testTimeProvider.onPhaseStart(ctx);
testTimeProvider.onPhaseStart(ctx.getCurrentPhase());
assertNull(testTimeProvider.getPhaseTookTime(searchPhaseName));
testTimeProvider.onPhaseFailure(ctx);
testTimeProvider.onPhaseFailure(ctx.getCurrentPhase());
assertNull(testTimeProvider.getPhaseTookTime(searchPhaseName));
}
}
Expand All @@ -43,11 +43,11 @@ public void testSearchTimeProviderPhaseEnd() {
for (SearchPhaseName searchPhaseName : SearchPhaseName.values()) {
when(mockSearchPhase.getSearchPhaseName()).thenReturn(searchPhaseName);
long tookTimeInMillis = randomIntBetween(1, 100);
testTimeProvider.onPhaseStart(ctx);
testTimeProvider.onPhaseStart(ctx.getCurrentPhase());
long startTime = System.nanoTime() - TimeUnit.MILLISECONDS.toNanos(tookTimeInMillis);
when(mockSearchPhase.getStartTimeInNanos()).thenReturn(startTime);
assertNull(testTimeProvider.getPhaseTookTime(searchPhaseName));
testTimeProvider.onPhaseEnd(ctx);
testTimeProvider.onPhaseEnd(ctx.getCurrentPhase());
assertThat(testTimeProvider.getPhaseTookTime(searchPhaseName), greaterThanOrEqualTo(tookTimeInMillis));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public void testShardLevelSearchGroupStats() throws Exception {
when(mockSearchPhase.getStartTimeInNanos()).thenReturn(System.nanoTime() - TimeUnit.SECONDS.toNanos(paramValue));
when(mockSearchPhase.getSearchPhaseName()).thenReturn(searchPhaseName);
for (int iterator = 0; iterator < paramValue; iterator++) {
testRequestStats.onPhaseStart(ctx);
testRequestStats.onPhaseEnd(ctx);
testRequestStats.onPhaseStart(ctx.getCurrentPhase());
testRequestStats.onPhaseEnd(ctx.getCurrentPhase());
}
}
searchStats1.setSearchRequestStats(testRequestStats);
Expand Down

0 comments on commit bd4ae8d

Please sign in to comment.