This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch add-sharding-attr in repository https://gitbox.apache.org/repos/asf/skywalking.git
commit ad25797adaea37325e91d60be9861cea669f0740 Author: Wu Sheng <[email protected]> AuthorDate: Fri Mar 18 17:16:58 2022 +0800 Add `Column.shardingKey` for column definition for BanyanDB --- CHANGES.md | 16 ++++++++++- codeStyle.xml | 1 + .../org/apache/skywalking/oal/rt/OALRuntime.java | 6 +++- .../manual/networkalias/NetworkAddressAlias.java | 2 +- .../EndpointRelationServerSideMetrics.java | 2 +- .../ServiceInstanceRelationServerSideMetrics.java | 2 +- .../service/ServiceRelationServerSideMetrics.java | 2 +- .../analysis/meter/function/HistogramFunction.java | 2 +- .../meter/function/PercentileFunction.java | 2 +- .../analysis/meter/function/avg/AvgFunction.java | 2 +- .../meter/function/avg/AvgHistogramFunction.java | 2 +- .../avg/AvgHistogramPercentileFunction.java | 2 +- .../meter/function/avg/AvgLabeledFunction.java | 2 +- .../meter/function/latest/LatestFunction.java | 2 +- .../analysis/meter/function/sum/SumFunction.java | 2 +- .../oap/server/core/source/ScopeDefaultColumn.java | 8 ++++++ .../oap/server/core/storage/annotation/Column.java | 32 ++++++++++++++++++---- .../oap/server/core/storage/model/ModelColumn.java | 26 +++++++++++++++++- .../server/core/storage/model/StorageModels.java | 31 +++++++++++++++------ .../server/core/storage/model/ModelColumnTest.java | 10 +++---- 20 files changed, 120 insertions(+), 34 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 52ba41e..05c5d23 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -31,6 +31,7 @@ Release Notes. * Update frontend-maven-plugin to 1.12 and npm to 16.14.0 for booster UI build. * Improve CI with the GHA new feature "run failed jobs". * Fix `./mvnw compile` not work if `./mvnw install` is not executed at least once. +* Add `JD_PRESERVE_LINE_FEEDS=true` in official code style file. #### OAP Server @@ -112,7 +113,8 @@ Release Notes. , `SW_CORE_REST_JETTY_DELTA`). * [Breaking Change] Remove configuration `graphql/path` (env var: `SW_QUERY_GRAPHQL_PATH`). * Add storage column attribute `indexOnly`, support ElasticSearch only index and not store some fields. -* Add `indexOnly=true` to `SegmentRecord.tags`, `AlarmRecord.tags`, `AbstractLogRecord.tags`, to reduce unnecessary storage. +* Add `indexOnly=true` to `SegmentRecord.tags`, `AlarmRecord.tags`, `AbstractLogRecord.tags`, to reduce unnecessary + storage. * [Breaking Change] Remove configuration `restMinThreads` (env var: `SW_CORE_REST_JETTY_MIN_THREADS` , `SW_RECEIVER_SHARING_JETTY_MIN_THREADS`). * Refactor the core Builder mechanism, new storage plugin could implement their own converter and get rid of hard @@ -120,6 +122,18 @@ Release Notes. * [Breaking Change] Break all existing 3rd-party storage extensions. * Remove hard requirement of BASE64 encoding for binary field. * Add complexity limitation for GraphQL query to avoid malicious query. +* Add `Column.shardingKey` for column definition for BanyanDB. + +``` +Sharding key is used to group time series data per metric of one entity. +For example, +ServiceA's traffic gauge, service call per minute, includes following timestamp values, then it should be +[ServiceA: 01-28 18:30 values-1, 01-28 18:31 values-2, 01-28 18:32 values-3, 01-28 18:32 values-4] + +BanyanDB is the 1st storage implementation supporting this. It would make continuous time series metrics stored closely and compressed better. + +NOTICE, this sharding concept is NOT for splitting data into different database instances or physical files. +``` #### UI diff --git a/codeStyle.xml b/codeStyle.xml index e4f0b3d..0db1200 100644 --- a/codeStyle.xml +++ b/codeStyle.xml @@ -21,6 +21,7 @@ </value> </option> <option name="JD_P_AT_EMPTY_LINES" value="false" /> + <option name="JD_PRESERVE_LINE_FEEDS" value="true" /> </JavaCodeStyleSettings> <ADDITIONAL_INDENT_OPTIONS fileType="haml"> <option name="INDENT_SIZE" value="2" /> diff --git a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java index 337805e..b67f522 100644 --- a/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java +++ b/oap-server/oal-rt/src/main/java/org/apache/skywalking/oal/rt/OALRuntime.java @@ -44,6 +44,7 @@ import javassist.bytecode.ClassFile; import javassist.bytecode.ConstPool; import javassist.bytecode.SignatureAttribute; import javassist.bytecode.annotation.Annotation; +import javassist.bytecode.annotation.BooleanMemberValue; import javassist.bytecode.annotation.ClassMemberValue; import javassist.bytecode.annotation.IntegerMemberValue; import javassist.bytecode.annotation.StringMemberValue; @@ -51,7 +52,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.JavaVersion; import org.apache.commons.lang3.SystemUtils; -import org.apache.skywalking.oap.server.library.util.StringUtil; import org.apache.skywalking.oal.rt.output.AllDispatcherContext; import org.apache.skywalking.oal.rt.output.DispatcherContext; import org.apache.skywalking.oal.rt.parser.AnalysisResult; @@ -75,6 +75,7 @@ import org.apache.skywalking.oap.server.core.storage.StorageException; import org.apache.skywalking.oap.server.core.storage.annotation.Column; import org.apache.skywalking.oap.server.library.module.ModuleStartException; import org.apache.skywalking.oap.server.library.util.ResourceUtils; +import org.apache.skywalking.oap.server.library.util.StringUtil; /** * OAL Runtime is the class generation engine, which load the generated classes from OAL scrip definitions. This runtime @@ -267,6 +268,9 @@ public class OALRuntime implements OALEngine { if (field.getType().equals(String.class)) { columnAnnotation.addMemberValue("length", new IntegerMemberValue(constPool, field.getLength())); } + if (field.isID()) { + columnAnnotation.addMemberValue("shardingKey", new BooleanMemberValue(true, constPool)); + } annotationsAttribute.addAnnotation(columnAnnotation); newField.getFieldInfo().addAttribute(annotationsAttribute); diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/networkalias/NetworkAddressAlias.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/networkalias/NetworkAddressAlias.java index 2cfe761..08cd42f 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/networkalias/NetworkAddressAlias.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/networkalias/NetworkAddressAlias.java @@ -51,7 +51,7 @@ public class NetworkAddressAlias extends Metrics { @Setter @Getter - @Column(columnName = ADDRESS) + @Column(columnName = ADDRESS, shardingKey = true) private String address; @Setter @Getter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/endpoint/EndpointRelationServerSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/endpoint/EndpointRelationServerSideMetrics.java index 886e715..55f947f 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/endpoint/EndpointRelationServerSideMetrics.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/endpoint/EndpointRelationServerSideMetrics.java @@ -58,7 +58,7 @@ public class EndpointRelationServerSideMetrics extends Metrics { private int componentId; @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Override diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/instance/ServiceInstanceRelationServerSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/instance/ServiceInstanceRelationServerSideMetrics.java index 0e26986..69d9751 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/instance/ServiceInstanceRelationServerSideMetrics.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/instance/ServiceInstanceRelationServerSideMetrics.java @@ -68,7 +68,7 @@ public class ServiceInstanceRelationServerSideMetrics extends Metrics { private int componentId; @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Override diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java index dbd307e..2e523bf 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/manual/relation/service/ServiceRelationServerSideMetrics.java @@ -58,7 +58,7 @@ public class ServiceRelationServerSideMetrics extends Metrics { private int componentId; @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Override diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/HistogramFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/HistogramFunction.java index 31f3df1..8221014 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/HistogramFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/HistogramFunction.java @@ -48,7 +48,7 @@ public abstract class HistogramFunction extends Meter implements AcceptableValue @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Getter @Setter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/PercentileFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/PercentileFunction.java index 74500f0..6048c5d 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/PercentileFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/PercentileFunction.java @@ -55,7 +55,7 @@ public abstract class PercentileFunction extends Meter implements AcceptableValu @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Getter @Setter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgFunction.java index b120c40..3c7e750 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgFunction.java @@ -50,7 +50,7 @@ public abstract class AvgFunction extends Meter implements AcceptableValue<Long> @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; /** diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramFunction.java index e32b54b..65a20b7 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramFunction.java @@ -59,7 +59,7 @@ public abstract class AvgHistogramFunction extends Meter implements AcceptableVa @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Getter @Setter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramPercentileFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramPercentileFunction.java index 4afcfc9..cf63668 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramPercentileFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgHistogramPercentileFunction.java @@ -74,7 +74,7 @@ public abstract class AvgHistogramPercentileFunction extends Meter implements Ac @Setter @Getter - @Column(columnName = ENTITY_ID) + @Column(columnName = ENTITY_ID, shardingKey = true) private String entityId; @Getter @Setter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgLabeledFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgLabeledFunction.java index faf5d99..58f5827 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgLabeledFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/avg/AvgLabeledFunction.java @@ -48,7 +48,7 @@ public abstract class AvgLabeledFunction extends Meter implements AcceptableValu @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; /** diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/latest/LatestFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/latest/LatestFunction.java index dbeff6d..7cf3cb6 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/latest/LatestFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/latest/LatestFunction.java @@ -47,7 +47,7 @@ public abstract class LatestFunction extends Meter implements AcceptableValue<Lo @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; /** diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/sum/SumFunction.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/sum/SumFunction.java index 5ecd35d..2c4ae90 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/sum/SumFunction.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/analysis/meter/function/sum/SumFunction.java @@ -47,7 +47,7 @@ public abstract class SumFunction extends Meter implements AcceptableValue<Long> @Setter @Getter - @Column(columnName = ENTITY_ID, length = 512) + @Column(columnName = ENTITY_ID, length = 512, shardingKey = true) private String entityId; @Setter diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/ScopeDefaultColumn.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/ScopeDefaultColumn.java index f2f9633..c24706c 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/ScopeDefaultColumn.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/source/ScopeDefaultColumn.java @@ -71,6 +71,14 @@ public class ScopeDefaultColumn { Class type(); + /** + * Declare this virtual column is representing an entity ID of this source and generated metrics. + * Typically, metric ID = timestamp + entity ID + * + * This takes {@link ISource#getEntityId()}'s return as the value. + * + * @return TRUE if this is an ID column. + */ boolean isID() default false; /** diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/Column.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/Column.java index b5ae39b..f310c75 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/Column.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/annotation/Column.java @@ -59,8 +59,8 @@ public @interface Column { boolean storageOnly() default false; /** - * The column(field) is just indexed, never stored. Note: this feature only supported by elasticsearch - * and don't support mappings update due to ElasticSearch server's limitation. + * The column(field) is just indexed, never stored. Note: this feature only supported by elasticsearch and don't + * support mappings update due to ElasticSearch server's limitation. * * NOTICE, metrics should not use this, as the OAP core merges indices of metrics automatically. */ @@ -70,17 +70,16 @@ public @interface Column { * @return the length of this column, this is only for {@link String} column. The usage of this depends on the * storage implementation. * - * Notice, different lengths may cause different types. - * Such as, over 16383 would make the type in MySQL to be MEDIUMTEXT, due to database varchar max=16383 - * + * Notice, different lengths may cause different types. Such as, over 16383 would make the type in MySQL to be + * MEDIUMTEXT, due to database varchar max=16383 * @since 7.1.0 */ int length() default 200; /** * The return name of system environment could provide an override value of the length limitation. - * @return the variable name of system environment. * + * @return the variable name of system environment. * @since 8.2.0 */ String lengthEnvVariable() default ""; @@ -103,6 +102,27 @@ public @interface Column { AnalyzerType analyzer() default AnalyzerType.OAP_ANALYZER; /** + * Sharding key is used to group time series data per metric of one entity. + * For example, + * ServiceA's traffic gauge, service call per minute, includes following timestamp values, then it should be + * [ServiceA: 01-28 18:30 values-1, 01-28 18:31 values-2, 01-28 18:32 values-3, 01-28 18:32 values-4] + * + * BanyanDB is the 1st storage implementation supporting this. It would make continuous time series metrics stored + * closely and compressed better. + * + * 1. One entity at most has one sharding key + * 2. If no column is appointed for this, {@link org.apache.skywalking.oap.server.core.storage.StorageData#id} + * would + * be used by the storage implementation accordingly. + * + * NOTICE, this sharding concept is NOT for splitting data into different database instances or physical files. + * + * @return TRUE if this column could(not must) be used for sharding. + * @since 9.0.0 + */ + boolean shardingKey() default false; + + /** * The analyzer declares the text analysis mode. */ enum AnalyzerType { diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumn.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumn.java index 9c3adb5..bdab637 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumn.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumn.java @@ -20,19 +20,41 @@ package org.apache.skywalking.oap.server.core.storage.model; import java.lang.reflect.Type; import lombok.Getter; +import lombok.ToString; import org.apache.skywalking.oap.server.core.analysis.metrics.DataTable; import org.apache.skywalking.oap.server.core.storage.annotation.Column; @Getter +@ToString public class ModelColumn { private final ColumnName columnName; private final Class<?> type; private final Type genericType; private final boolean matchQuery; + /** + * Storage this column for query result, but can't be as a condition . Conflict with {@link #indexOnly} + */ private final boolean storageOnly; + /** + * Index this column for query condition only. Conflict with {@link #storageOnly} + * + * @since 9.0.0 + */ private final boolean indexOnly; + /** + * The max length of column value for length sensitive database. + */ private final int length; + /** + * The analyzer policy appointed to fuzzy query, especially for ElasticSearch + */ private final Column.AnalyzerType analyzer; + /** + * Sharding key is used to group time series data per metric of one entity. See {@link Column#shardingKey()}. + * + * @since 9.0.0 + */ + private boolean isShardingKey; public ModelColumn(ColumnName columnName, Class<?> type, @@ -42,7 +64,8 @@ public class ModelColumn { boolean indexOnly, boolean isValue, int length, - Column.AnalyzerType analyzer) { + Column.AnalyzerType analyzer, + boolean isShardingKey) { this.columnName = columnName; this.type = type; this.genericType = genericType; @@ -67,5 +90,6 @@ public class ModelColumn { "The column " + columnName + " can't be defined as both indexOnly and storageOnly."); } this.indexOnly = indexOnly; + this.isShardingKey = isShardingKey; } } diff --git a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java index 606337a..dfb2297 100644 --- a/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java +++ b/oap-server/server-core/src/main/java/org/apache/skywalking/oap/server/core/storage/model/StorageModels.java @@ -24,7 +24,6 @@ import java.util.HashMap; import java.util.List; import java.util.Objects; import lombok.extern.slf4j.Slf4j; -import org.apache.skywalking.oap.server.library.util.StringUtil; import org.apache.skywalking.oap.server.core.analysis.FunctionCategory; import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; import org.apache.skywalking.oap.server.core.storage.StorageException; @@ -34,6 +33,7 @@ import org.apache.skywalking.oap.server.core.storage.annotation.QueryUnifiedInde import org.apache.skywalking.oap.server.core.storage.annotation.Storage; import org.apache.skywalking.oap.server.core.storage.annotation.SuperDataset; import org.apache.skywalking.oap.server.core.storage.annotation.ValueColumnMetadata; +import org.apache.skywalking.oap.server.library.util.StringUtil; /** * StorageModels manages all models detected by the core. @@ -106,6 +106,7 @@ public class StorageModels implements IModelManager, ModelCreator, ModelManipula Field[] fields = clazz.getDeclaredFields(); + ModelColumn existingShardingColumn = null; for (Field field : fields) { if (field.isAnnotationPresent(Column.class)) { Column column = field.getAnnotation(Column.class); @@ -125,14 +126,28 @@ public class StorageModels implements IModelManager, ModelCreator, ModelManipula } } } - modelColumns.add( - new ModelColumn( - new ColumnName(modelName, column.columnName()), field.getType(), field.getGenericType(), - column.matchQuery(), column.storageOnly(), column.indexOnly(), column.dataType().isValue(), columnLength, - column.analyzer() - )); + + final ModelColumn modelColumn = new ModelColumn( + new ColumnName(modelName, column.columnName()), field.getType(), field.getGenericType(), + column.matchQuery(), column.storageOnly(), column.indexOnly(), column.dataType().isValue(), + columnLength, + column.analyzer(), column.shardingKey() + ); + if (column.shardingKey()) { + if (existingShardingColumn == null) { + existingShardingColumn = modelColumn; + } else { + log.error("Entity {} have 2 columns declaring as the sharding key. {}:{}", modelName, + existingShardingColumn, modelColumn + ); + throw new IllegalStateException( + "Entity " + modelName + " have 2 columns declaring as the sharding key."); + } + } + + modelColumns.add(modelColumn); if (log.isDebugEnabled()) { - log.debug("The field named {} with the {} type", column.columnName(), field.getType()); + log.debug("The field named [{}] with the [{}] type", column.columnName(), field.getType()); } if (column.dataType().isValue()) { ValueColumnMetadata.INSTANCE.putIfAbsent( diff --git a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumnTest.java b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumnTest.java index 8ca4dd4..7095396 100644 --- a/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumnTest.java +++ b/oap-server/server-core/src/test/java/org/apache/skywalking/oap/server/core/storage/model/ModelColumnTest.java @@ -28,14 +28,14 @@ public class ModelColumnTest { public void testColumnDefine() { ModelColumn column = new ModelColumn(new ColumnName("", "abc"), byte[].class, byte[].class, true, false, false, true, 0, - Column.AnalyzerType.OAP_ANALYZER + Column.AnalyzerType.OAP_ANALYZER, false ); Assert.assertEquals(true, column.isStorageOnly()); Assert.assertEquals("abc", column.getColumnName().getName()); column = new ModelColumn(new ColumnName("", "abc"), DataTable.class, DataTable.class, true, false, false, true, 200, - Column.AnalyzerType.OAP_ANALYZER + Column.AnalyzerType.OAP_ANALYZER, false ); Assert.assertEquals(true, column.isStorageOnly()); Assert.assertEquals("abc", column.getColumnName().getName()); @@ -43,7 +43,7 @@ public class ModelColumnTest { column = new ModelColumn(new ColumnName("", "abc"), String.class, String.class, true, false, false, true, 200, - Column.AnalyzerType.OAP_ANALYZER + Column.AnalyzerType.OAP_ANALYZER, false ); Assert.assertEquals(false, column.isStorageOnly()); Assert.assertEquals("abc", column.getColumnName().getName()); @@ -53,7 +53,7 @@ public class ModelColumnTest { public void testConflictDefinition() { ModelColumn column = new ModelColumn(new ColumnName("", "abc"), String.class, String.class, true, true, false, true, 200, - Column.AnalyzerType.OAP_ANALYZER + Column.AnalyzerType.OAP_ANALYZER, false ); } @@ -61,7 +61,7 @@ public class ModelColumnTest { public void testConflictDefinitionIndexOnly() { ModelColumn column = new ModelColumn(new ColumnName("", "abc"), String.class, String.class, true, true, true, false, 200, - Column.AnalyzerType.OAP_ANALYZER + Column.AnalyzerType.OAP_ANALYZER, false ); } }
