Skip to content

Commit

Permalink
[improve] use eclipselink orm replace of hibernate orm (#1801)
Browse files Browse the repository at this point in the history
Signed-off-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Logic <zqr10159@dromara.org>
  • Loading branch information
3 people authored Apr 23, 2024
1 parent 997bbe3 commit fed2f53
Show file tree
Hide file tree
Showing 74 changed files with 1,786 additions and 1,335 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ node_modules
# debug env
application-dev.yml
application-mysql.yml
application-pg.yml
17 changes: 0 additions & 17 deletions alerter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- data jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
</dependency>
<!-- kafka -->
<dependency>
<groupId>org.apache.kafka</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void reduceAndSendAlarm(Alert alert) {
List<Tag> tagList = alertMonitorDao.findMonitorIdBindTags(monitorId);
for (Tag tag : tagList) {
if (!tags.containsKey(tag.getName())) {
tags.put(tag.getName(), tag.getValue());
tags.put(tag.getName(), tag.getTagValue());
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions collector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-remoting</artifactId>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- run sql script-->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
13 changes: 11 additions & 2 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,25 @@
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
</dependency>
<!-- jackson -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.hertzbeat.common.config;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.persistence.config.SessionCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.sessions.Session;

/**
* change column name that from upper-case to lower-case-with-underscore
*/
@Slf4j
public class EclipseLinkCustomizer implements SessionCustomizer {

@Override
public void customize(Session session) throws Exception {
for (ClassDescriptor descriptor : session.getDescriptors().values()) {
for (DatabaseMapping mapping : descriptor.getMappings()) {
if (mapping.isDirectToFieldMapping()) {
// update the column name to lower case with underscore
if (!mapping.getField().getName().equalsIgnoreCase(mapping.getAttributeName())) {
// already custom define the column name, ignore
continue;
}
// todo here i try to change column name that from upper-case to lower-case-with-underscore
// but failed, when update name, something relate exception happen more
mapping.getField().setName(convertCamelCase(mapping.getAttributeName()));
}
}
}

}

private String convertCamelCase(String camelCase) {
StringBuilder result = new StringBuilder();
if (camelCase != null && !camelCase.isEmpty()) {
result.append(Character.toLowerCase(camelCase.charAt(0)));
for (int i = 1; i < camelCase.length(); i++) {
char currentChar = camelCase.charAt(i);
if (Character.isUpperCase(currentChar)) {
result.append('_').append(Character.toLowerCase(currentChar));
} else {
result.append(currentChar);
}
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.hertzbeat.common.config;

import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;

/**
* jpa eclipselink impl config
*/
@Configuration
@EnableAutoConfiguration
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {

protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
super(dataSource, properties, jtaTransactionManager);
}

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}

@Override
protected Map<String, Object> getVendorProperties() {
HashMap<String, Object> map = new HashMap<>(8);
map.put(PersistenceUnitProperties.DDL_GENERATION, "create-or-extend-tables");
map.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, "org.apache.hertzbeat.common.config.EclipseLinkCustomizer");
map.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
map.put(PersistenceUnitProperties.WEAVING, "true");
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@
import jakarta.persistence.Transient;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand All @@ -61,9 +60,7 @@
public class Alert {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "snow-flake-id")
@GenericGenerator(name = "snow-flake-id",
strategy = "org.apache.hertzbeat.common.util.SnowFlakeIdGenerator")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Schema(title = "Alarm record entity primary key index ID",
description = "Alarm record entity primary key index ID",
example = "87584674384", accessMode = READ_ONLY)
Expand All @@ -72,7 +69,7 @@ public class Alert {
@Schema(title = "Alert target object: monitor availability-available metrics-app.metrics.field",
description = "Alert target object: monitor availability-available metrics-app.metrics.field",
example = "1", accessMode = READ_WRITE)
@Length(max = 255)
@Size(max = 255)
private String target;

@Schema(title = "Alarm definition ID associated with the alarm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import jakarta.persistence.Table;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
Expand All @@ -39,7 +40,6 @@
import org.apache.hertzbeat.common.entity.manager.JsonByteListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -69,7 +69,7 @@ public class AlertConverge {

@Schema(title = "Policy name", description = "Policy name",
example = "converge-1", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AllArgsConstructor;
Expand All @@ -40,7 +41,6 @@
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -69,24 +69,24 @@ public class AlertDefine {
private Long id;

@Schema(title = "Monitoring Type", example = "linux", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String app;

@Schema(title = "Monitoring Metrics", example = "cpu", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String metric;

@Schema(title = "Monitoring Metrics Field", example = "usage", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
private String field;

@Schema(title = "Is Apply All Default", example = "false", accessMode = READ_WRITE)
private boolean preset;

@Schema(title = "Alarm Threshold Expr", example = "usage>90", accessMode = READ_WRITE)
@Length(max = 2048)
@Size(max = 2048)
@Column(length = 2048)
private String expr;

Expand Down Expand Up @@ -117,7 +117,7 @@ public class AlertDefine {

@Schema(title = "Alarm Template", example = "linux {monitor_name}: {monitor_id} cpu usage high",
accessMode = READ_WRITE)
@Length(max = 2048)
@Size(max = 2048)
@Column(length = 2048)
private String template;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.entity.manager.Monitor;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand Down Expand Up @@ -86,6 +84,7 @@ public class AlertDefineMonitorBind {
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "monitor_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
// todo instead of @NotFound
// @NotFound(action = NotFoundAction.IGNORE)
private Monitor monitor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
Expand All @@ -39,7 +40,7 @@
import org.apache.hertzbeat.common.entity.manager.JsonByteListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.hibernate.validator.constraints.Length;
import org.apache.hertzbeat.common.entity.manager.ZonedDateTimeAttributeConverter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class AlertSilence {
@Schema(title = "Policy name",
description = "Policy name",
example = "silence-1", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String name;

Expand Down Expand Up @@ -113,9 +114,11 @@ public class AlertSilence {
private List<Byte> days;

@Schema(title = "Limit time period start", example = "00:00:00", accessMode = READ_WRITE)
@Convert(converter = ZonedDateTimeAttributeConverter.class)
private ZonedDateTime periodStart;

@Schema(title = "Restricted time period end", example = "23:59:59", accessMode = READ_WRITE)
@Convert(converter = ZonedDateTimeAttributeConverter.class)
private ZonedDateTime periodEnd;

@Schema(title = "The creator of this record", example = "tom", accessMode = READ_ONLY)
Expand Down
Loading

0 comments on commit fed2f53

Please sign in to comment.