baiyangtx commented on code in PR #3748:
URL: https://github.com/apache/amoro/pull/3748#discussion_r2299565960


##########
amoro-ams/src/main/java/org/apache/amoro/server/table/DefaultTableRuntimeStore.java:
##########
@@ -0,0 +1,282 @@
+/*
+ * 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.amoro.server.table;
+
+import org.apache.amoro.ServerTableIdentifier;
+import org.apache.amoro.exception.AmoroRuntimeException;
+import org.apache.amoro.exception.PersistenceException;
+import org.apache.amoro.server.persistence.NestedSqlSession;
+import org.apache.amoro.server.persistence.PersistentBase;
+import org.apache.amoro.server.persistence.TableRuntimeMeta;
+import org.apache.amoro.server.persistence.TableRuntimeState;
+import org.apache.amoro.server.persistence.mapper.TableMetaMapper;
+import org.apache.amoro.server.persistence.mapper.TableRuntimeMapper;
+import org.apache.amoro.shade.guava32.com.google.common.base.Preconditions;
+import org.apache.amoro.shade.guava32.com.google.common.collect.Lists;
+import org.apache.amoro.shade.guava32.com.google.common.collect.Sets;
+import org.apache.amoro.table.StateKey;
+import org.apache.amoro.table.TableRuntimeStore;
+import org.apache.amoro.table.TableSummary;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+public class DefaultTableRuntimeStore extends PersistentBase implements 
TableRuntimeStore {
+  private static final Logger LOG = 
LoggerFactory.getLogger(DefaultTableRuntimeStore.class);
+  private final Lock tableLock = new ReentrantLock();
+  private final ServerTableIdentifier tableIdentifier;
+  private final TableRuntimeMeta meta;
+  private final Map<String, TableRuntimeState> states = new 
ConcurrentHashMap<>();
+
+  public DefaultTableRuntimeStore(
+      ServerTableIdentifier tableIdentifier,
+      TableRuntimeMeta meta,
+      List<StateKey<?>> requiredStates,
+      List<TableRuntimeState> restoredStates) {
+    Preconditions.checkNotNull(tableIdentifier, "ServerTableIdentifier must 
not be null.");
+    Preconditions.checkNotNull(meta, "TableRuntimeMeta must not be null.");
+    Preconditions.checkNotNull(requiredStates, "requiredStates must not be 
null.");
+    Preconditions.checkNotNull(restoredStates, "restoredStates must not be 
null.");
+    this.tableIdentifier = tableIdentifier;
+    this.meta = meta;
+    restoreStates(requiredStates, restoredStates);
+  }
+
+  @Override
+  public ServerTableIdentifier getTableIdentifier() {
+    return tableIdentifier;
+  }
+
+  @Override
+  public String getGroupName() {
+    return this.meta.getGroupName();
+  }
+
+  @Override
+  public Map<String, String> getTableConfig() {
+    return this.meta.getTableConfig();
+  }
+
+  @Override
+  public int getStatusCode() {
+    return this.meta.getStatusCode();
+  }
+
+  @Override
+  public <T> T getState(StateKey<T> key) {
+    Preconditions.checkNotNull(key, "TableRuntime state key cannot be null");
+    Preconditions.checkNotNull(
+        states.containsKey(key.getKey()), "TableRuntime state %s not 
initialized", key);
+    TableRuntimeState tableState = states.get(key.getKey());
+    return key.deserialize(tableState.getStateValue());
+  }
+
+  @Override
+  public void synchronizedInvoke(Runnable runnable) {
+    tableLock.lock();
+    try {
+      runnable.run();
+    } finally {
+      tableLock.unlock();
+    }
+  }
+
+  @Override
+  public void dispose() {
+    doAsTransaction(
+        () -> doAs(TableRuntimeMapper.class, m -> 
m.deleteRuntime(tableIdentifier.getId())),
+        () -> doAs(TableRuntimeMapper.class, m -> 
m.removeAllTableStates(tableIdentifier.getId())),
+        () -> doAs(TableMetaMapper.class, m -> 
m.deleteTableIdById(tableIdentifier.getId())));
+  }
+
+  @Override
+  public TableRuntimeOperation begin() {
+    return new TableRuntimeOperationImpl();
+  }
+
+  protected void restoreStates(
+      List<StateKey<?>> requiredStates, List<TableRuntimeState> 
restoredStates) {
+    Map<String, TableRuntimeState> stateMap =
+        restoredStates.stream()
+            .collect(Collectors.toMap(TableRuntimeState::getStateKey, 
Function.identity()));
+
+    requiredStates.forEach(
+        key -> {
+          if (stateMap.containsKey(key.getKey())) {
+            states.put(key.getKey(), stateMap.get(key.getKey()));
+            stateMap.remove(key.getKey());
+          } else {
+            doAs(
+                TableRuntimeMapper.class,
+                mapper ->
+                    mapper.saveState(
+                        tableIdentifier.getId(), key.getKey(), 
key.serializeDefault()));
+            TableRuntimeState state =
+                getAs(
+                    TableRuntimeMapper.class,
+                    m -> m.getState(tableIdentifier.getId(), key.getKey()));
+            Preconditions.checkNotNull(state, "State %s initialize failed", 
key.getKey());
+            states.put(key.getKey(), state);
+          }
+        });
+
+    if (!stateMap.isEmpty()) {
+      LOG.warn("Found {} useless runtime states for {}", stateMap.size(), 
tableIdentifier);
+      stateMap.forEach(
+          (k, s) -> {
+            LOG.warn("Remove useless runtime state {} for {}", k, 
tableIdentifier);
+            doAs(TableRuntimeMapper.class, m -> m.removeState(s.getStateId()));
+          });
+    }
+  }
+
+  private class TableRuntimeOperationImpl implements TableRuntimeOperation {
+
+    private final TableRuntimeMeta oldMeta;
+    private final List<Runnable> opeartions = Lists.newArrayList();

Review Comment:
   tks, I have fix it.



-- 
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]

Reply via email to