Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openjdk.jmh.annotations.Warmup;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.ignite.internal.MessageSerializationContext.IGNORED;
import static org.openjdk.jmh.annotations.Mode.Throughput;

/** Benchmarks the {@link DirectMessageReader} compressed-field hot path. */
Expand Down Expand Up @@ -96,7 +97,7 @@ public void setup() {

writer.setBuffer(buf);

boolean finished = writer.writeMessage(msg, true);
boolean finished = writer.writeMessage(msg, true, IGNORED);

if (!finished)
throw new IllegalStateException("Message does not fit into the buffer.");
Expand All @@ -111,7 +112,7 @@ public Message compressedMessage() {

reader.setBuffer(buf);

Message msg = reader.readMessage(true);
Message msg = reader.readMessage(true, IGNORED);

reader.reset();

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.ignite.internal;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.ignite.internal.processors.rollingupgrade.feature.IgniteFeature;

/**
* Links the annotated class to the specified {@link IgniteFeature} registry. The registry
* is used to resolve fully qualified names of features that introduced or deprecated fields
* (see {@link Order#introducedBy()} and {@link Order#deprecatedBy()}).
*
* <p>If this annotation is absent, the Ignite Core Feature Registry is used.</p>
*
* @see Order
* @see IgniteFeature
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface FeatureRegistry {
/** @return Class of the feature registry. */
Class<?> value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,19 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;
import org.apache.ignite.internal.systemview.SystemViewRowAttributeWalkerProcessor;
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.lang.IgniteBiTuple;
import org.jetbrains.annotations.Nullable;

import static org.apache.ignite.internal.MessageSerializerGenerator.DLFT_ENUM_MAPPER_CLS;
import static org.apache.ignite.internal.MessageSerializerGenerator.enumType;
import static org.apache.ignite.internal.MessageSerializerGenerator.qualifiedClassName;

/**
* Annotation processor that generates serialization and deserialization code for classes implementing the {@code Message} interface.
Expand Down Expand Up @@ -96,6 +99,13 @@ public class MessageProcessor extends AbstractProcessor {
/** Checked exception declared by the generated methods. */
static final String IGNITE_CHECKED_EXCEPTION_CLS = "org.apache.ignite.IgniteCheckedException";

/** Feature registry a message resolves its guards against unless it declares one with {@link FeatureRegistry}. */
static final String DFLT_FEATURE_REG_CLS =
"org.apache.ignite.internal.processors.rollingupgrade.feature.SupportedFeatureRegistry";

/** */
static final String IGNITE_FEATURE_CLS = "org.apache.ignite.internal.processors.rollingupgrade.feature.IgniteFeature";

/** */
public static final String GRID_H2_NULL = "org.apache.ignite.internal.processors.query.h2.twostep.msg.GridH2Null";

Expand All @@ -105,10 +115,14 @@ public class MessageProcessor extends AbstractProcessor {
/** */
public static final Set<String> NO_PUBLIC_CTOR_MSGS = Set.of(GRID_H2_NULL, ZK_NO_SERVERS_MESSAGE);

/** Messages with no fields. A serializer generation intentionally skipped. */
/** */
static final String OP_CTX_SNAPSHOT_MESSAGE_CLASS = "org.apache.ignite.internal.thread.context.OperationContextSnapshotMessage";

/** Messages with no fields, or with a hand-written serializer. A serializer generation intentionally skipped. */
static final String[] SKIP_MESSAGES = {
"org.apache.ignite.internal.processors.odbc.ClientMessage",
COMPRESSED_MESSAGE_CLASS,
OP_CTX_SNAPSHOT_MESSAGE_CLASS,
"org.apache.ignite.loadtests.communication.GridTestMessage",
"org.apache.ignite.spi.communication.tcp.TestDelayMessage"
};
Expand Down Expand Up @@ -425,4 +439,99 @@ TypeMirror type(String clazz) {
TypeElement typeElement = elementUtils.getTypeElement(clazz);
return typeElement != null ? typeElement.asType() : null;
}

/** */
@Nullable public static FieldFeatureGuard buildFieldFeatureGuard(ProcessingEnvironment env, VariableElement field) {
Order ann = field.getAnnotation(Order.class);

String introducingFeature = ann.introducedBy();
String deprecatingFeature = ann.deprecatedBy();

if (introducingFeature.isEmpty() && deprecatingFeature.isEmpty())
return null;

if (introducingFeature.equals(deprecatingFeature)) {
printError(env, field, "Elements introducedBy and deprecatedBy of the @Order annotation must not reference the same feature.");

return null;
}

String regCls = resolveFeatureRegistry(field.getEnclosingElement());

String regName = regCls.substring(regCls.lastIndexOf('.') + 1);

List<String> conditions = new ArrayList<>();

if (!introducingFeature.isEmpty()) {
validateFeature(env, field, introducingFeature, regCls);

conditions.add("ctx.includeFieldIntroducedBy(" + regName + '.' + introducingFeature + ")");
}

if (!deprecatingFeature.isEmpty()) {
validateFeature(env, field, deprecatingFeature, regCls);

conditions.add("ctx.includeFieldDeprecatedBy(" + regName + '.' + deprecatingFeature + ")");
}

return new FieldFeatureGuard(regCls, String.join(" && ", conditions));
}

/** */
private static void validateFeature(ProcessingEnvironment env, VariableElement field, String featureName, String regCls) {
TypeElement regElem = env.getElementUtils().getTypeElement(regCls);

if (regElem == null) {
printError(env, field, "Cannot resolve the feature registry class [reg=" + regCls + ']');

return;
}

for (Element featureElem : regElem.getEnclosedElements()) {
if (featureElem.getKind() != ElementKind.FIELD || !featureElem.getSimpleName().contentEquals(featureName))
continue;

Set<Modifier> mods = featureElem.getModifiers();

if (!mods.contains(Modifier.PUBLIC) || !mods.contains(Modifier.STATIC) || !mods.contains(Modifier.FINAL))
printError(env, field, "Feature constant must be public static final [reg=" + regCls + ", feature=" + featureName + ']');
else if (!isIgniteFeature(env, featureElem))
printError(env, field, "Feature constant must be of type IgniteFeature [reg=" + regCls + ", feature=" + featureName + ']');

return;
}

printError(env, field,
"Failed to resolve feature in the registry by its name [reg=" + regCls + ", feature=" + featureName + ']');
}

/** */
private static boolean isIgniteFeature(ProcessingEnvironment env, Element featureElem) {
TypeElement igniteFeatureType = env.getElementUtils().getTypeElement(IGNITE_FEATURE_CLS);

return igniteFeatureType != null && env.getTypeUtils().isAssignable(featureElem.asType(), igniteFeatureType.asType());
}

/** */
private static void printError(ProcessingEnvironment env, Element el, String msg) {
env.getMessager().printMessage(Diagnostic.Kind.ERROR, msg, el);
}

/** */
private static String resolveFeatureRegistry(Element cls) {
FeatureRegistry ann = cls.getAnnotation(FeatureRegistry.class);

if (ann == null)
return DFLT_FEATURE_REG_CLS;

try {
return ann.value().getName();
}
catch (MirroredTypeException e) {
return qualifiedClassName(e.getTypeMirror());
}
}

/** */
public record FieldFeatureGuard(String registry, String expression) { }
}
Loading
Loading