Java Code Examples for org.elasticsearch.cluster.metadata.MetaData#Custom

The following examples show how to use org.elasticsearch.cluster.metadata.MetaData#Custom . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClusterChangedEvent.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a set of custom meta data types when any custom metadata for the cluster has changed
 * between the previous cluster state and the new cluster state. custom meta data types are
 * returned iff they have been added, updated or removed between the previous and the current state
 */
public Set<String> changedCustomMetaDataSet() {
    Set<String> result = new HashSet<>();
    ImmutableOpenMap<String, MetaData.Custom> currentCustoms = state.metaData().customs();
    ImmutableOpenMap<String, MetaData.Custom> previousCustoms = previousState.metaData().customs();
    if (currentCustoms.equals(previousCustoms) == false) {
        for (ObjectObjectCursor<String, MetaData.Custom> currentCustomMetaData : currentCustoms) {
            // new custom md added or existing custom md changed
            if (previousCustoms.containsKey(currentCustomMetaData.key) == false
                    || currentCustomMetaData.value.equals(previousCustoms.get(currentCustomMetaData.key)) == false) {
                result.add(currentCustomMetaData.key);
            }
        }
        // existing custom md deleted
        for (ObjectObjectCursor<String, MetaData.Custom> previousCustomMetaData : previousCustoms) {
            if (currentCustoms.containsKey(previousCustomMetaData.key) == false) {
                result.add(previousCustomMetaData.key);
            }
        }
    }
    return result;
}
 
Example 2
Source File: MetaDataUpgrader.java    From crate with Apache License 2.0 6 votes vote down vote up
public MetaDataUpgrader(Collection<UnaryOperator<Map<String, MetaData.Custom>>> customMetaDataUpgraders,
                        Collection<UnaryOperator<Map<String, IndexTemplateMetaData>>> indexTemplateMetaDataUpgraders) {
    this.customMetaDataUpgraders = customs -> {
        Map<String, MetaData.Custom> upgradedCustoms = new HashMap<>(customs);
        for (UnaryOperator<Map<String, MetaData.Custom>> customMetaDataUpgrader : customMetaDataUpgraders) {
            upgradedCustoms = customMetaDataUpgrader.apply(upgradedCustoms);
        }
        return upgradedCustoms;
    };

    this.indexTemplateMetaDataUpgraders = templates -> {
        Map<String, IndexTemplateMetaData> upgradedTemplates = new HashMap<>(templates);
        for (UnaryOperator<Map<String, IndexTemplateMetaData>> upgrader : indexTemplateMetaDataUpgraders) {
            upgradedTemplates = upgrader.apply(upgradedTemplates);
        }
        return upgradedTemplates;
    };
}
 
Example 3
Source File: TestCustomMetaData.java    From crate with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T extends MetaData.Custom> T fromXContent(Function<String, MetaData.Custom> supplier, XContentParser parser)
    throws IOException {
    XContentParser.Token token;
    String data = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            String currentFieldName = parser.currentName();
            if ("data".equals(currentFieldName)) {
                if (parser.nextToken() != XContentParser.Token.VALUE_STRING) {
                    throw new ElasticsearchParseException("failed to parse snapshottable metadata, invalid data type");
                }
                data = parser.text();
            } else {
                throw new ElasticsearchParseException("failed to parse snapshottable metadata, unknown field [{}]", currentFieldName);
            }
        } else {
            throw new ElasticsearchParseException("failed to parse snapshottable metadata");
        }
    }
    if (data == null) {
        throw new ElasticsearchParseException("failed to parse snapshottable metadata, data not found");
    }
    return (T) supplier.apply(data);
}
 
Example 4
Source File: PrivilegesMetaDataUpgraderTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testExistingUserWithoutAnyPrivilegeGetsAllPrivileges() throws Exception {
    Map<String, MetaData.Custom> customMap = new HashMap<>(1);
    customMap.put(UsersMetaData.TYPE, new UsersMetaData(UserDefinitions.SINGLE_USER_ONLY));
    Map<String, MetaData.Custom> oldCustomMap = new HashMap<>(customMap);

    Map<String, MetaData.Custom> newCustomMap = UPGRADER.apply(Settings.EMPTY, customMap);
    assertThat(newCustomMap, not(is(oldCustomMap)));

    UsersPrivilegesMetaData privilegesMetaData = (UsersPrivilegesMetaData) newCustomMap.get(UsersPrivilegesMetaData.TYPE);
    assertThat(privilegesMetaData, notNullValue());
    Set<Privilege> userPrivileges = privilegesMetaData.getUserPrivileges("Arthur");
    assertThat(userPrivileges, notNullValue());
    Set<Privilege.Type> privilegeTypes = userPrivileges.stream().map(p -> p.ident().type()).collect(Collectors.toSet());
    assertThat(privilegeTypes, containsInAnyOrder(Privilege.Type.values()));
}
 
Example 5
Source File: CustomMetaDataUpgraderLoader.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, MetaData.Custom> apply(Map<String, MetaData.Custom> metaDataCustoms) {
    while (upgraders.hasNext()) {
        upgraders.next().apply(settings, metaDataCustoms);
    }
    return metaDataCustoms;
}
 
Example 6
Source File: PrivilegesMetaDataUpgrader.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, MetaData.Custom> apply(Settings settings, Map<String, MetaData.Custom> customMetaData) {
    UsersMetaData usersMetaData = (UsersMetaData) customMetaData.get(UsersMetaData.TYPE);
    if (usersMetaData == null) {
        return customMetaData;
    }
    List<String> users = usersMetaData.userNames();
    if (users.size() == 0) {
        return customMetaData;
    }
    UsersPrivilegesMetaData privilegesMetaData =
        (UsersPrivilegesMetaData) customMetaData.get(UsersPrivilegesMetaData.TYPE);
    if (privilegesMetaData == null) {
        privilegesMetaData = new UsersPrivilegesMetaData();
        customMetaData.put(UsersPrivilegesMetaData.TYPE, privilegesMetaData);
    }
    for (String userName : usersMetaData.userNames()) {
        Set<Privilege> userPrivileges = privilegesMetaData.getUserPrivileges(userName);
        if (userPrivileges == null) {
            userPrivileges = new HashSet<>();
            privilegesMetaData.createPrivileges(userName, userPrivileges);

            // add GRANT privileges for all available types on the CLUSTER class
            for (Privilege.Type privilegeType : Privilege.Type.values()) {
                userPrivileges.add(
                    new Privilege(
                        Privilege.State.GRANT,
                        privilegeType,
                        Privilege.Clazz.CLUSTER,
                        null,
                        CRATE_USER.name()));
            }
        }
    }
    return customMetaData;
}
 
Example 7
Source File: PrivilegesMetaDataUpgraderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoUsersNothingChanged() throws Exception {
    Map<String, MetaData.Custom> customMap = new HashMap<>(1);
    customMap.put(UsersMetaData.TYPE, new UsersMetaData(ImmutableMap.of()));
    Map<String, MetaData.Custom> oldCustomMap = new HashMap<>(customMap);
    Map<String, MetaData.Custom> newCustomMap = UPGRADER.apply(Settings.EMPTY, customMap);
    assertThat(newCustomMap, is(oldCustomMap));
}
 
Example 8
Source File: PrivilegesMetaDataUpgraderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testExistingUserWithPrivilegesDoesntGetMore() throws Exception {
    Map<String, MetaData.Custom> customMap = new HashMap<>(1);
    customMap.put(UsersMetaData.TYPE, new UsersMetaData(UserDefinitions.SINGLE_USER_ONLY));
    customMap.put(UsersPrivilegesMetaData.TYPE, new UsersPrivilegesMetaData(
        MapBuilder.<String, Set<Privilege>>newMapBuilder()
            .put("Arthur", Sets.newHashSet(
                new Privilege(Privilege.State.GRANT, Privilege.Type.DQL, Privilege.Clazz.CLUSTER, null, "crate")))
            .map()));
    Map<String, MetaData.Custom> oldCustomMap = new HashMap<>(customMap);

    Map<String, MetaData.Custom> newCustomMap = UPGRADER.apply(Settings.EMPTY, customMap);
    assertThat(newCustomMap, is(oldCustomMap));
}
 
Example 9
Source File: PrivilegesMetaDataUpgraderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testExistingUserWithEmptyPrivilegesDoesntGetMore() throws Exception {
    Map<String, MetaData.Custom> customMap = new HashMap<>(1);
    customMap.put(UsersMetaData.TYPE, new UsersMetaData(UserDefinitions.SINGLE_USER_ONLY));
    customMap.put(UsersPrivilegesMetaData.TYPE, new UsersPrivilegesMetaData(
        MapBuilder.<String, Set<Privilege>>newMapBuilder()
            .put("Arthur", Collections.emptySet())
            .map()));
    Map<String, MetaData.Custom> oldCustomMap = new HashMap<>(customMap);

    Map<String, MetaData.Custom> newCustomMap = UPGRADER.apply(Settings.EMPTY, customMap);
    assertThat(newCustomMap, is(oldCustomMap));
}
 
Example 10
Source File: ClusterModule.java    From crate with Apache License 2.0 4 votes vote down vote up
private static <T extends MetaData.Custom> void registerMetaDataCustom(List<Entry> entries, String name, Reader<? extends T> reader,
                                                                   Reader<NamedDiff> diffReader) {
    registerCustom(entries, MetaData.Custom.class, name, reader, diffReader);
}
 
Example 11
Source File: TestCustomMetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
public static NamedDiff<MetaData.Custom> readDiffFrom(String name, StreamInput in)  throws IOException {
    return readDiffFrom(MetaData.Custom.class, name, in);
}
 
Example 12
Source File: Plugin.java    From crate with Apache License 2.0 2 votes vote down vote up
/**
 * Provides a function to modify global custom meta data on startup.
 * <p>
 * Plugins should return the input custom map via {@link UnaryOperator#identity()} if no upgrade is required.
 * <p>
 * The order of custom meta data upgraders calls is undefined and can change between runs so, it is expected that
 * plugins will modify only data owned by them to avoid conflicts.
 * <p>
 * @return Never {@code null}. The same or upgraded {@code MetaData.Custom} map.
 * @throws IllegalStateException if the node should not start because at least one {@code MetaData.Custom}
 *                               is unsupported
 */
public UnaryOperator<Map<String, MetaData.Custom>> getCustomMetaDataUpgrader() {
    return UnaryOperator.identity();
}