/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch 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. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.cluster.routing; import org.opensearch.common.Nullable; import org.opensearch.core.ParseField; import org.opensearch.common.UUIDs; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.core.xcontent.ObjectParser; import org.opensearch.core.xcontent.ToXContentObject; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; import java.io.IOException; import java.util.Objects; /** * Uniquely identifies an allocation. An allocation is a shard moving from unassigned to initializing, * or relocation. *
* Relocation is a special case, where the origin shard is relocating with a relocationId and same id, and
* the target shard (only materialized in RoutingNodes) is initializing with the id set to the origin shard
* relocationId. Once relocation is done, the new allocation id is set to the relocationId. This is similar
* behavior to how ShardRouting#currentNodeId is used.
*
* @opensearch.internal
*/
public class AllocationId implements ToXContentObject, Writeable {
private static final String ID_KEY = "id";
private static final String RELOCATION_ID_KEY = "relocation_id";
private static final ObjectParser
* Note that this is expected to be called on the allocation id
* of the *source* shard
*/
public static AllocationId cancelRelocation(AllocationId allocationId) {
assert allocationId.getRelocationId() != null;
return new AllocationId(allocationId.getId(), null);
}
/**
* Creates a new allocation id finalizing a relocation.
*
* Note that this is expected to be called on the allocation id
* of the *target* shard and thus it only needs to clear the relocating id.
*/
public static AllocationId finishRelocation(AllocationId allocationId) {
assert allocationId.getRelocationId() != null;
return new AllocationId(allocationId.getId(), null);
}
/**
* The allocation id uniquely identifying an allocation, note, if it is relocation
* the {@link #getRelocationId()} need to be taken into account as well.
*/
public String getId() {
return id;
}
/**
* The transient relocation id holding the unique id that is used for relocation.
*/
public String getRelocationId() {
return relocationId;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AllocationId that = (AllocationId) o;
if (!id.equals(that.id)) {
return false;
}
return !(relocationId != null ? !relocationId.equals(that.relocationId) : that.relocationId != null);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + (relocationId != null ? relocationId.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "[id=" + id + (relocationId == null ? "" : ", rId=" + relocationId) + "]";
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(ID_KEY, id);
if (relocationId != null) {
builder.field(RELOCATION_ID_KEY, relocationId);
}
builder.endObject();
return builder;
}
public static AllocationId fromXContent(XContentParser parser) throws IOException {
return ALLOCATION_ID_PARSER.parse(parser, new AllocationId.Builder(), null).build();
}
}