Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that content IDs are unique in a Nessie repository #7757

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@ as necessary. Empty sections will not end in the release notes.

- Nessie Quarkus parts are now built against Java 17 and Java 17 is required to run Nessie Quarkus Server directly.
If you use the Docker image, nothing needs to be done, because the image already contains a compatible Java runtime.
- Due to the introduction of extensible object types in the storage layer, some storage backends
- Due to the introduction of new object types in the storage layer, some storage backends
will require a schema upgrade:
- JDBC: the following SQL statement must be executed on the Nessie database (please adapt the
statement to the actual database SQL dialect):
```sql
ALTER TABLE objs ADD COLUMN x_class VARCHAR, ADD COLUMN x_data BYTEA, ADD COLUMN x_compress VARCHAR;
ALTER TABLE objs
ADD COLUMN x_class VARCHAR,
ADD COLUMN x_data BYTEA,
ADD COLUMN x_compress VARCHAR,
ADD COLUMN u_space VARCHAR,
ADD COLUMN u_value VARCHAR;
```
- Cassandra: the following CQL statement must be executed on the Nessie database and keyspace:
```cql
ALTER TABLE <keyspace>.objs ADD x_class text, ADD x_data blob, ADD x_compress text;
ALTER TABLE <keyspace>.objs
ADD x_class text,
ADD x_data blob,
ADD x_compress text,
ADD u_space text,
ADD u_value text;
```

### Breaking changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class ObjSerializers {
RefObjSerializer.INSTANCE,
StringObjSerializer.INSTANCE,
TagObjSerializer.INSTANCE,
UniqueIdObjSerializer.INSTANCE,
CustomObjSerializer.INSTANCE);

static {
Expand Down Expand Up @@ -75,6 +76,9 @@ public static ObjSerializer<Obj> forType(@Nonnull @jakarta.annotation.Nonnull Ob
case VALUE:
serializer = ContentValueObjSerializer.INSTANCE;
break;
case UNIQUE:
serializer = UniqueIdObjSerializer.INSTANCE;
break;
default:
throw new IllegalArgumentException("Unknown standard object type: " + type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2023 Dremio
*
* Licensed 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.projectnessie.versioned.storage.cassandra.serializers;

import static org.projectnessie.versioned.storage.cassandra.CassandraConstants.INSERT_OBJ_PREFIX;
import static org.projectnessie.versioned.storage.cassandra.CassandraConstants.INSERT_OBJ_VALUES;
import static org.projectnessie.versioned.storage.cassandra.CassandraConstants.STORE_OBJ_SUFFIX;
import static org.projectnessie.versioned.storage.common.objtypes.UniqueIdObj.uniqueId;

import com.datastax.oss.driver.api.core.cql.BoundStatementBuilder;
import com.datastax.oss.driver.api.core.cql.Row;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.projectnessie.versioned.storage.cassandra.CqlColumn;
import org.projectnessie.versioned.storage.cassandra.CqlColumnType;
import org.projectnessie.versioned.storage.common.exceptions.ObjTooLargeException;
import org.projectnessie.versioned.storage.common.objtypes.UniqueIdObj;
import org.projectnessie.versioned.storage.common.persist.ObjId;

public class UniqueIdObjSerializer implements ObjSerializer<UniqueIdObj> {

public static final ObjSerializer<UniqueIdObj> INSTANCE = new UniqueIdObjSerializer();

private static final CqlColumn COL_UNIQUE_SPACE = new CqlColumn("u_space", CqlColumnType.VARCHAR);
private static final CqlColumn COL_UNIQUE_VALUE = new CqlColumn("u_value", CqlColumnType.VARCHAR);

private static final Set<CqlColumn> COLS = ImmutableSet.of(COL_UNIQUE_SPACE, COL_UNIQUE_VALUE);

private static final String INSERT_CQL =
INSERT_OBJ_PREFIX
+ COLS.stream().map(CqlColumn::name).collect(Collectors.joining(","))
+ INSERT_OBJ_VALUES
+ COLS.stream().map(c -> ":" + c.name()).collect(Collectors.joining(","))
+ ")";

private static final String STORE_CQL = INSERT_CQL + STORE_OBJ_SUFFIX;

private UniqueIdObjSerializer() {}

@Override
public Set<CqlColumn> columns() {
return COLS;
}

@Override
public String insertCql(boolean upsert) {
return upsert ? INSERT_CQL : STORE_CQL;
}

@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public void serialize(
UniqueIdObj obj,
BoundStatementBuilder stmt,
int incrementalIndexLimit,
int maxSerializedIndexSize)
throws ObjTooLargeException {
stmt.setString(COL_UNIQUE_SPACE.name(), obj.space());
stmt.setString(COL_UNIQUE_VALUE.name(), obj.value());
}

@Override
public UniqueIdObj deserialize(Row row, ObjId id) {
return uniqueId(
id, row.getString(COL_UNIQUE_SPACE.name()), row.getString(COL_UNIQUE_VALUE.name()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ message StringProto {
bytes text = 54;
}

message UniqueIdProto {
string space = 1;
string value = 2;
}

enum CommitTypeProto {
T_UNKNOWN = 0;
NORMAL = 1;
Expand Down Expand Up @@ -108,6 +113,7 @@ message ObjProto {
StringProto string_data = 7;
TagProto tag = 8;
CustomProto custom = 9;
UniqueIdProto uniqueId = 10;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.projectnessie.versioned.storage.common.objtypes.RefObj.ref;
import static org.projectnessie.versioned.storage.common.objtypes.StringObj.stringData;
import static org.projectnessie.versioned.storage.common.objtypes.TagObj.tag;
import static org.projectnessie.versioned.storage.common.objtypes.UniqueIdObj.uniqueId;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -49,6 +50,7 @@
import org.projectnessie.versioned.storage.common.objtypes.StandardObjType;
import org.projectnessie.versioned.storage.common.objtypes.StringObj;
import org.projectnessie.versioned.storage.common.objtypes.TagObj;
import org.projectnessie.versioned.storage.common.objtypes.UniqueIdObj;
import org.projectnessie.versioned.storage.common.persist.ImmutableReference;
import org.projectnessie.versioned.storage.common.persist.Obj;
import org.projectnessie.versioned.storage.common.persist.ObjId;
Expand All @@ -68,6 +70,7 @@
import org.projectnessie.versioned.storage.common.proto.StorageTypes.StringProto;
import org.projectnessie.versioned.storage.common.proto.StorageTypes.Stripe;
import org.projectnessie.versioned.storage.common.proto.StorageTypes.TagProto;
import org.projectnessie.versioned.storage.common.proto.StorageTypes.UniqueIdProto;

public final class ProtoSerialization {

Expand Down Expand Up @@ -230,6 +233,8 @@ public static byte[] serializeObj(Obj obj, int incrementalIndexSizeLimit, int in
return b.setStringData(serializeStringData((StringObj) obj)).build().toByteArray();
case TAG:
return b.setTag(serializeTag((TagObj) obj)).build().toByteArray();
case UNIQUE:
return b.setUniqueId(serializeUniqueId((UniqueIdObj) obj)).build().toByteArray();
default:
throw new UnsupportedOperationException("Unknown standard object type " + obj.type());
}
Expand Down Expand Up @@ -284,6 +289,9 @@ public static Obj deserializeObjProto(ObjId id, ObjProto obj) {
if (obj.hasTag()) {
return deserializeTag(id, obj.getTag());
}
if (obj.hasUniqueId()) {
return deserializeUniqueId(id, obj.getUniqueId());
}
if (obj.hasCustom()) {
return deserializeCustom(id, obj.getCustom());
}
Expand Down Expand Up @@ -493,6 +501,14 @@ private static TagProto.Builder serializeTag(TagObj obj) {
return tag;
}

private static UniqueIdObj deserializeUniqueId(ObjId id, UniqueIdProto uniqueId) {
return uniqueId(id, uniqueId.getSpace(), uniqueId.getValue());
}

private static UniqueIdProto.Builder serializeUniqueId(UniqueIdObj obj) {
return UniqueIdProto.newBuilder().setSpace(obj.space()).setValue(obj.value());
}

private static Obj deserializeCustom(ObjId id, CustomProto custom) {
return SmileSerialization.deserializeObj(
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.REF;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.STRING;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.TAG;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.UNIQUE;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.VALUE;
import static org.projectnessie.versioned.storage.common.objtypes.StringObj.stringData;
import static org.projectnessie.versioned.storage.common.objtypes.TagObj.tag;
import static org.projectnessie.versioned.storage.common.objtypes.UniqueIdObj.uniqueId;
import static org.projectnessie.versioned.storage.common.persist.ObjId.EMPTY_OBJ_ID;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromString;
import static org.projectnessie.versioned.storage.common.persist.ObjId.randomObjId;
Expand Down Expand Up @@ -440,6 +442,7 @@ public static Stream<Obj> allObjectTypeSamples() {
copyFromUtf8("This is not a markdown")),
ref(randomObjId(), "foo", randomObjId(), 123L, null),
ref(randomObjId(), "bar", randomObjId(), 456L, randomObjId()),
uniqueId(randomObjId(), "space", "value"),
// custom object types
SimpleCustomObj.builder()
.id(randomObjId())
Expand Down Expand Up @@ -481,6 +484,8 @@ static StandardObjType typeDifferentThan(ObjType type) {
return STRING;
case STRING:
return INDEX_SEGMENTS;
case UNIQUE:
return REF;
default:
// fall through
}
Expand Down Expand Up @@ -901,6 +906,8 @@ public static Obj updateObjChange(Obj obj) {
"filename",
asList(randomObjId(), randomObjId(), randomObjId(), randomObjId()),
ByteString.copyFrom(new byte[123]));
case UNIQUE:
return uniqueId(obj.id(), "other_space", "other_value");
default:
// fall through
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.REF;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.STRING;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.TAG;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.UNIQUE;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.VALUE;

import com.google.common.hash.Hasher;
Expand Down Expand Up @@ -122,4 +123,10 @@ static ObjId stringDataHash(
hasher.putBytes(text.asReadOnlyByteBuffer());
return hashAsObjId(hasher);
}

static ObjId uniqueIdHash(String space, String value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One general remark: wouldn't it be simpler to model the value as an opaque byte array? It seems we could save some conversions to and from string in the common case where the value is an UUID.

Hasher hasher =
newHasher().putString(UNIQUE.name(), UTF_8).putString(space, UTF_8).putString(value, UTF_8);
return hashAsObjId(hasher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ public enum StandardObjType implements ObjType {
INDEX_SEGMENTS("I", IndexSegmentsObj.class),

/** {@link Obj} is a {@link IndexObj}. */
INDEX("i", IndexObj.class);
INDEX("i", IndexObj.class),

/** Obj is a {@link UniqueIdObj}. */
UNIQUE("u", UniqueIdObj.class),
;

private final String shortName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2022 Dremio
*
* Licensed 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.projectnessie.versioned.storage.common.objtypes;

import static org.projectnessie.versioned.storage.common.objtypes.Hashes.uniqueIdHash;

import javax.annotation.Nullable;
import org.immutables.value.Value;
import org.projectnessie.versioned.storage.common.logic.ReferenceLogic;
import org.projectnessie.versioned.storage.common.persist.Obj;
import org.projectnessie.versioned.storage.common.persist.ObjId;
import org.projectnessie.versioned.storage.common.persist.ObjType;
import org.projectnessie.versioned.storage.common.persist.Persist;

/**
* Describes the <em>internal</em> state of a reference when it has been created, managed by {@link
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The javadoc seems to refer to RefObj.

* ReferenceLogic} implementations, not available/tracked for transactional {@link Persist}
* instances.
*/
@Value.Immutable
public interface UniqueIdObj extends Obj {

@Override
default ObjType type() {
return StandardObjType.UNIQUE;
}

@Override
@Value.Parameter(order = 1)
@Nullable
@jakarta.annotation.Nullable
ObjId id();

/** The "ID space", for example {@code content-id}. */
@Value.Parameter(order = 2)
String space();

/** The value of the ID within the {@link #space()}. */
@Value.Parameter(order = 3)
String value();

static UniqueIdObj uniqueId(ObjId id, String space, String value) {
return ImmutableUniqueIdObj.of(id, space, value);
}

static UniqueIdObj uniqueId(String space, String value) {
return uniqueId(uniqueIdHash(space, value), space, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.REF;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.STRING;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.TAG;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.UNIQUE;
import static org.projectnessie.versioned.storage.common.objtypes.StandardObjType.VALUE;
import static org.projectnessie.versioned.storage.common.objtypes.StringObj.stringData;
import static org.projectnessie.versioned.storage.common.objtypes.TagObj.tag;
import static org.projectnessie.versioned.storage.common.objtypes.UniqueIdObj.uniqueId;
import static org.projectnessie.versioned.storage.common.persist.ObjId.EMPTY_OBJ_ID;
import static org.projectnessie.versioned.storage.common.persist.ObjId.objIdFromString;
import static org.projectnessie.versioned.storage.common.persist.ObjId.randomObjId;
Expand Down Expand Up @@ -101,7 +103,8 @@ static Stream<Arguments> standardObjTypes() {
STRING),
arguments(indexSegments(randomObjId(), emptyList()), INDEX_SEGMENTS),
arguments(
index(randomObjId(), emptyImmutableIndex(COMMIT_OP_SERIALIZER).serialize()), INDEX));
index(randomObjId(), emptyImmutableIndex(COMMIT_OP_SERIALIZER).serialize()), INDEX),
arguments(uniqueId(randomObjId(), "space", "value"), UNIQUE));
}

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class ObjSerializers {
RefObjSerializer.INSTANCE,
StringObjSerializer.INSTANCE,
TagObjSerializer.INSTANCE,
UniqueIdObjSerializer.INSTANCE,
CustomObjSerializer.INSTANCE);

static {
Expand Down Expand Up @@ -75,6 +76,9 @@ public static ObjSerializer<Obj> forType(@Nonnull @jakarta.annotation.Nonnull Ob
case VALUE:
serializer = ContentValueObjSerializer.INSTANCE;
break;
case UNIQUE:
serializer = UniqueIdObjSerializer.INSTANCE;
break;
default:
throw new IllegalArgumentException("Unknown standard object type: " + type);
}
Expand Down