Skip to content

Commit

Permalink
[core] Throw SnapshotNotExistException if the snapshot for creating t…
Browse files Browse the repository at this point in the history
…ag does not exist (#3951)
  • Loading branch information
LinMingQiang authored Aug 14, 2024
1 parent fff9a31 commit e1c4db9
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.paimon.utils.BranchManager;
import org.apache.paimon.utils.SegmentsCache;
import org.apache.paimon.utils.SnapshotManager;
import org.apache.paimon.utils.SnapshotNotExistException;
import org.apache.paimon.utils.TagManager;

import javax.annotation.Nullable;
Expand All @@ -79,7 +80,6 @@

import static org.apache.paimon.CoreOptions.PATH;
import static org.apache.paimon.utils.Preconditions.checkArgument;
import static org.apache.paimon.utils.Preconditions.checkNotNull;

/** Abstract {@link FileStoreTable}. */
abstract class AbstractFileStoreTable implements FileStoreTable {
Expand Down Expand Up @@ -502,7 +502,7 @@ public void rollbackTo(long snapshotId) {
rollbackHelper().cleanLargerThan(snapshotManager.snapshot(snapshotId));
}

public Snapshot createTagInternal(long fromSnapshotId) {
public Snapshot findSnapshot(long fromSnapshotId) throws SnapshotNotExistException {
SnapshotManager snapshotManager = snapshotManager();
Snapshot snapshot = null;
if (snapshotManager.snapshotExists(fromSnapshotId)) {
Expand All @@ -518,35 +518,39 @@ public Snapshot createTagInternal(long fromSnapshotId) {
}
}
}
checkArgument(
snapshot != null,
"Cannot create tag because given snapshot #%s doesn't exist.",
fromSnapshotId);

SnapshotNotExistException.checkNotNull(
snapshot,
String.format(
"Cannot create tag because given snapshot #%s doesn't exist.",
fromSnapshotId));

return snapshot;
}

@Override
public void createTag(String tagName, long fromSnapshotId) {
createTag(
tagName, createTagInternal(fromSnapshotId), coreOptions().tagDefaultTimeRetained());
createTag(tagName, findSnapshot(fromSnapshotId), coreOptions().tagDefaultTimeRetained());
}

@Override
public void createTag(String tagName, long fromSnapshotId, Duration timeRetained) {
createTag(tagName, createTagInternal(fromSnapshotId), timeRetained);
createTag(tagName, findSnapshot(fromSnapshotId), timeRetained);
}

@Override
public void createTag(String tagName) {
Snapshot latestSnapshot = snapshotManager().latestSnapshot();
checkNotNull(latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
SnapshotNotExistException.checkNotNull(
latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
createTag(tagName, latestSnapshot, coreOptions().tagDefaultTimeRetained());
}

@Override
public void createTag(String tagName, Duration timeRetained) {
Snapshot latestSnapshot = snapshotManager().latestSnapshot();
checkNotNull(latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
SnapshotNotExistException.checkNotNull(
latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
createTag(tagName, latestSnapshot, timeRetained);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.paimon.utils;

import org.apache.paimon.Snapshot;

/** Thrown by TagManager or SnapshotManager when the specified snapshotId does not exist. */
public class SnapshotNotExistException extends RuntimeException {

private static final long serialVersionUID = 1L;

public SnapshotNotExistException(long snapshotId) {
super("Snapshot " + snapshotId + " does not exist");
}

public SnapshotNotExistException(String errMsg) {
super(errMsg);
}

public static void checkNotNull(Snapshot snapshotId, String errMsg) {
if (snapshotId == null) {
throw new SnapshotNotExistException(errMsg);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.paimon.flink.procedure;

import org.apache.paimon.flink.CatalogITCaseBase;
import org.apache.paimon.utils.SnapshotNotExistException;

import org.apache.flink.types.Row;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;

/** IT Case for {@link CreateTagProcedure}. */
public class CreateTagsProcedureITCase extends CatalogITCaseBase {

@Test
public void testCreateTags() {
sql(
"CREATE TABLE T ("
+ " k STRING,"
+ " dt STRING,"
+ " PRIMARY KEY (k, dt) NOT ENFORCED"
+ ") PARTITIONED BY (dt) WITH ("
+ " 'bucket' = '1'"
+ ")");
sql("insert into T values('k', '2024-01-01')");
sql("insert into T values('k2', '2024-01-02')");

sql("CALL sys.create_tag('default.T', 'tag1')");

assertThat(
sql("select * from T /*+ OPTIONS('scan.tag-name'='tag1') */").stream()
.map(Row::toString))
.containsExactlyInAnyOrder("+I[k2, 2024-01-02]", "+I[k, 2024-01-01]");

sql("CALL sys.create_tag('default.T', 'tag2', 1)");

assertThat(
sql("select * from T /*+ OPTIONS('scan.tag-name'='tag2') */").stream()
.map(Row::toString))
.containsExactlyInAnyOrder("+I[k, 2024-01-01]");
}

@Test
public void testThrowSnapshotNotExistException() {
sql(
"CREATE TABLE T ("
+ " k STRING,"
+ " dt STRING,"
+ " PRIMARY KEY (k, dt) NOT ENFORCED"
+ ") PARTITIONED BY (dt) WITH ("
+ " 'bucket' = '1'"
+ ")");

assertThatException()
.isThrownBy(() -> sql("CALL sys.create_tag('default.T', 'tag1')"))
.withRootCauseInstanceOf(SnapshotNotExistException.class)
.withMessageContaining("Cannot create tag because latest snapshot doesn't exist.");

assertThatException()
.isThrownBy(() -> sql("CALL sys.create_tag('default.T', 'tag1', 1)"))
.withRootCauseInstanceOf(SnapshotNotExistException.class)
.withMessageContaining(
"Cannot create tag because given snapshot #1 doesn't exist.");
}
}

0 comments on commit e1c4db9

Please sign in to comment.