Pochatkin commented on code in PR #2044: URL: https://github.com/apache/ignite-3/pull/2044#discussion_r1196489634
########## modules/code-deployment/src/main/java/org/apache/ignite/internal/deployunit/metastore/DeploymentUnitMetastoreImpl.java: ########## @@ -0,0 +1,199 @@ +/* + * 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.ignite.internal.deployunit.metastore; + +import static java.util.concurrent.CompletableFuture.completedFuture; +import static org.apache.ignite.internal.deployunit.metastore.key.UnitKey.allUnits; +import static org.apache.ignite.internal.deployunit.metastore.key.UnitKey.clusterStatusKey; +import static org.apache.ignite.internal.deployunit.metastore.key.UnitKey.nodeStatusKey; +import static org.apache.ignite.internal.deployunit.metastore.key.UnitKey.nodes; +import static org.apache.ignite.internal.deployunit.metastore.key.UnitMetaSerializer.deserialize; +import static org.apache.ignite.internal.deployunit.metastore.key.UnitMetaSerializer.serialize; +import static org.apache.ignite.internal.metastorage.dsl.Conditions.exists; +import static org.apache.ignite.internal.metastorage.dsl.Conditions.notExists; +import static org.apache.ignite.internal.metastorage.dsl.Conditions.revision; +import static org.apache.ignite.internal.metastorage.dsl.Operations.noop; +import static org.apache.ignite.internal.metastorage.dsl.Operations.put; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import org.apache.ignite.internal.deployunit.UnitMeta; +import org.apache.ignite.internal.deployunit.UnitStatus; +import org.apache.ignite.internal.deployunit.version.Version; +import org.apache.ignite.internal.metastorage.MetaStorageManager; +import org.apache.ignite.internal.metastorage.dsl.Condition; +import org.apache.ignite.internal.metastorage.dsl.Conditions; +import org.apache.ignite.internal.metastorage.dsl.Operation; +import org.apache.ignite.internal.metastorage.dsl.Operations; +import org.apache.ignite.internal.rest.api.deployment.DeploymentStatus; +import org.apache.ignite.lang.ByteArray; + +/** + * Implementation of {@link DeploymentUnitMetastore} based on {@link MetaStorageManager}. + */ +public class DeploymentUnitMetastoreImpl implements DeploymentUnitMetastore { + private final MetaStorageManager metaStorage; + + public DeploymentUnitMetastoreImpl(MetaStorageManager metaStorage) { + this.metaStorage = metaStorage; + } + + @Override + public CompletableFuture<UnitMeta> getClusterStatus(String id, Version version) { + return metaStorage.get(clusterStatusKey(id, version)).thenApply(entry -> { + byte[] value = entry.value(); + if (value == null) { + return null; + } + + return deserialize(value); + }); + } + + @Override + public CompletableFuture<UnitMeta> getNodeStatus(String id, Version version, String nodeId) { + return metaStorage.get(nodeStatusKey(id, version, nodeId)).thenApply(entry -> { + byte[] value = entry.value(); + if (value == null) { + return null; + } + + return deserialize(value); + }); + } + + @Override + public CompletableFuture<List<UnitStatus>> getAllClusterStatuses() { + CompletableFuture<List<UnitStatus>> result = new CompletableFuture<>(); + metaStorage.prefix(allUnits()).subscribe(new UnitsAccumulator().toSubscriber(result)); + return result; + } + + private CompletableFuture<List<UnitStatus>> getClusterStatuses(Predicate<UnitMeta> filter) { + CompletableFuture<List<UnitStatus>> result = new CompletableFuture<>(); + metaStorage.prefix(allUnits()).subscribe(new UnitsAccumulator(filter).toSubscriber(result)); + return result; + } + + @Override + public CompletableFuture<UnitStatus> getClusterStatuses(String id) { + CompletableFuture<UnitStatus> result = new CompletableFuture<>(); + metaStorage.prefix(allUnits()).subscribe(new ClusterStatusAccumulator(id).toSubscriber(result)); + return result; + } + + @Override + public CompletableFuture<Boolean> createClusterStatus(String id, Version version) { + ByteArray key = clusterStatusKey(id, version); + byte[] value = serialize(new UnitMeta(id, version, DeploymentStatus.UPLOADING)); + + return metaStorage.invoke(notExists(key), put(key, value), noop()); + } + + @Override + public CompletableFuture<Boolean> createNodeStatus(String id, Version version, String nodeId) { + ByteArray key = nodeStatusKey(id, version, nodeId); + byte[] value = serialize(new UnitMeta(id, version, DeploymentStatus.UPLOADING)); + return metaStorage.invoke(notExists(key), put(key, value), noop()); + } + + @Override + public CompletableFuture<Boolean> updateClusterStatus(String id, Version version, DeploymentStatus status) { + return updateStatus(clusterStatusKey(id, version), status); + } + + @Override + public CompletableFuture<Boolean> updateNodeStatus(String id, Version version, String nodeId, DeploymentStatus status) { + return updateStatus(nodeStatusKey(id, version, nodeId), status); + } + + @Override + public CompletableFuture<List<UnitStatus>> findAllByNodeConsistentId(String nodeId) { + CompletableFuture<List<String>> result = new CompletableFuture<>(); + metaStorage.prefix(nodes()).subscribe(new UnitsByNodeAccumulator(nodeId).toSubscriber(result)); + return result.thenCompose(ids -> getClusterStatuses(meta -> ids.contains(meta.id()))); + } + + @Override + public CompletableFuture<Boolean> remove(String id, Version version) { + ByteArray key = clusterStatusKey(id, version); + CompletableFuture<List<byte[]>> nodesFuture = new CompletableFuture<>(); + metaStorage.prefix(nodes(id, version)).subscribe(new KeyAccumulator().toSubscriber(nodesFuture)); + + return nodesFuture.thenCompose(nodes -> + metaStorage.invoke(existsAll(key, nodes), removeAll(key, nodes), Collections.emptyList()) + ); + } + + private Condition existsAll(ByteArray key, List<byte[]> keys) { + Condition result = exists(key); Review Comment: This is private static method. Why it should be inlined into lyambda? -- 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]
