bitflicker64 commented on code in PR #3331: URL: https://github.com/apache/hugegraph/pull/3331#discussion_r4261508870
########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/LoadingMode.java: ########## @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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. + */ + +package org.apache.hugegraph; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; + +import org.apache.hugegraph.type.define.GraphMode; +import org.apache.hugegraph.util.E; + +import com.google.common.collect.ImmutableMap; + +/** Same graph-instance ownership; this state is neither persisted nor distributed. */ +public final class LoadingMode { + + private static final int MAX_RELEASED = 1024; + + private final String instance = UUID.randomUUID().toString(); + private final Map<String, String> active = new HashMap<>(); + private final Map<String, String> released = new HashMap<>(); + private long epoch; + private volatile GraphMode mode = GraphMode.NONE; + + public GraphMode mode() { + return this.mode; + } + + public synchronized void mode(GraphMode mode) { + Objects.requireNonNull(mode, "mode"); + E.checkState(this.active.isEmpty(), "Loading owners are active; release them before setting graph mode"); Review Comment: Minor: the expected rejections of this protocol reach the client as HTTP 500. `E.checkState` throws `IllegalStateException`, and `ExceptionFilter` maps only `HugeException` and `IllegalArgumentException` to 400, so this one falls through to `UnknownExceptionMapper` and `INTERNAL_SERVER_ERROR`. That covers an enum `PUT .../mode` while owners are active (this line, a new 500 case on an existing endpoint), and in `update` an `acquire` on a manually set mode and a principal mismatch. These are caller conflicts, not Server faults; a 500 invites blind retries and error alerts, and the status becomes part of the contract once the Loader ships. `StandardHugeGraph.mode` also logs `Graph {} will work in {} mode` before the setter can reject the change. Requested change: report these with a 4xx (throw `HugeException` or `IllegalArgumentException`, or map them to 409 in `GraphsAPI`), update the `IllegalStateException` assertions in `LoadingModeTest` to match, and move the log line after the s uccessful set. ########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/LoadingMode.java: ########## @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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. + */ + +package org.apache.hugegraph; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; + +import org.apache.hugegraph.type.define.GraphMode; +import org.apache.hugegraph.util.E; + +import com.google.common.collect.ImmutableMap; + +/** Same graph-instance ownership; this state is neither persisted nor distributed. */ +public final class LoadingMode { + + private static final int MAX_RELEASED = 1024; + + private final String instance = UUID.randomUUID().toString(); + private final Map<String, String> active = new HashMap<>(); + private final Map<String, String> released = new HashMap<>(); + private long epoch; + private volatile GraphMode mode = GraphMode.NONE; + + public GraphMode mode() { + return this.mode; + } + + public synchronized void mode(GraphMode mode) { + Objects.requireNonNull(mode, "mode"); + E.checkState(this.active.isEmpty(), "Loading owners are active; release them before setting graph mode"); + this.mode = mode; + } + + public synchronized Map<String, Object> capability() { + return ImmutableMap.of("protocol", 1, "instance", this.instance, "epoch", this.epoch); + } + + public synchronized Map<String, Object> update(String action, String instance, long epoch, + String token, String principal) { + E.checkArgument("acquire".equals(action) || "release".equals(action), + "Loading owner action must be acquire or release"); + E.checkArgument(this.instance.equals(instance), "Loading owner belongs to another graph instance"); + E.checkArgument(epoch >= 0 && epoch <= this.epoch, "Invalid loading owner epoch"); + E.checkArgument(token != null && token.length() == 36 && UUID.fromString(token).toString().equals(token), + "Loading owner token must be a canonical UUID"); + Objects.requireNonNull(principal, "principal"); + String key = epoch + "/" + token; + String owner = this.active.get(key); + if (owner != null) { + E.checkState(owner.equals(principal), "Loading owner belongs to another authenticated principal"); Review Comment: Important: an owner left behind by a killed Loader cannot be cleared by an operator, and the graph then stays in `LOADING` until the graph instance is reopened (a Server restart, or a graph reload where one is available). A release needs the exact `instance`/`epoch`/`token` and, on this line, the acquiring principal; `mode(GraphMode)` on line 47 rejects every enum `PUT .../mode` while `active` is non-empty; line 74 is the only place an entry leaves `active`. The Server never returns the token after the acquire receipt (`capability()` has protocol, instance and epoch only), and the paired Loader (apache/hugegraph-toolchain#815 at 63cfd76) prints the tuple only when a release fails, so after `kill -9` the handle is lost. Checked on `LoadingMode` compiled from this head: after `acquire` by `loader-user`, `release` of the same tuple by `admin` throws `Loading owner belongs to another authenticated principal`, and `mode(GraphMode.NONE)` throws `Loading owners are active`. While the own er is stuck, `PUT .../mode` cannot set `RESTORING` or `MERGING`, and `GraphTransaction.prepareAdditions` keeps skipping `checkNonnullProperty` for all vertex writes. Once a Loader uses `acquire`, the enum `PUT .../mode` with `NONE` that used to clear a crashed import no longer works. `docs/server-runtime.md` documents reopening the graph instance as the recovery, so this is a request to revisit that choice, not a lease or automatic expiry: add a manual privileged override, for example let a space or admin role release any owner or clear all owners of a graph, or list the active `epoch/token` and principal in `GET .../mode/owners` for that role, with a test for the path chosen. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
