Skip to content

Commit

Permalink
Adds deprecation logging to ScriptDocValues#getValues.
Browse files Browse the repository at this point in the history
First commit addressing issue #22919.

`ScriptDocValues#getValues` was added for backwards compatibility but no
longer needed. Scripts using the syntax `doc.foo.values.bar` should be
using `doc.foo.bar` instead.
  • Loading branch information
j-haj committed Oct 4, 2018
1 parent d7893fd commit b8f383a
Showing 1 changed file with 41 additions and 0 deletions.
Expand Up @@ -26,15 +26,20 @@
import org.elasticsearch.common.geo.GeoHashUtils;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.geo.GeoUtils;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;

import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.AbstractList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.UnaryOperator;

/**
Expand All @@ -48,6 +53,25 @@
*/
public abstract class ScriptDocValues<T> extends AbstractList<T> {

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(ESLoggerFactory.getLogger(ScriptDocValues.class));
/**
* Callback for deprecated fields. In production this should always point to
* {@link #deprecationLogger} but tests will override it so they can test
* that we use the required permissions when calling it.
*/
private final BiConsumer<String, String> deprecationCallback;

public ScriptDocValues() {
deprecationCallback = deprecationLogger::deprecatedAndMaybeLog;
}

/**
* Constructor for testing deprecation callback.
*/
ScriptDocValues(BiConsumer<String, String> deprecationCallback) {
this.deprecationCallback = deprecationCallback;
}

/**
* Set the current doc ID.
*/
Expand All @@ -57,6 +81,8 @@ public abstract class ScriptDocValues<T> extends AbstractList<T> {
* Return a copy of the list of the values for the current document.
*/
public final List<T> getValues() {
deprecated("ScriptDocValues#getValues", "Deprecated getValues used, values should be accessed directly "
+ "(e.g., doc.foo.bar instead of doc.foo.values.bar)");
return this;
}

Expand Down Expand Up @@ -86,6 +112,21 @@ public final void sort(Comparator<? super T> c) {
throw new UnsupportedOperationException("doc values are unmodifiable");
}

/**
* Log a deprecation log, with the server's permissions and not the permissions
* of the script calling this method. We need to do this to prevent errors
* when rolling the log file.
*/
private void deprecated(String key, String message) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
deprecationCallback.accept(key, message);
return null;
}
});
}

public static final class Longs extends ScriptDocValues<Long> {
private final SortedNumericDocValues in;
private long[] values = new long[0];
Expand Down

0 comments on commit b8f383a

Please sign in to comment.