Skip to content

Commit

Permalink
Merge pull request #1084 from msvinaykumar/deletepartitions
Browse files Browse the repository at this point in the history
delete Partitions RM to MVP
  • Loading branch information
dinogun authored Jan 30, 2024
2 parents fc881e5 + a17944c commit 5d64285
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 12 deletions.
102 changes: 102 additions & 0 deletions design/KruizeConfiguration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Kruize Configuration

The following environment variables are set using the `kubectl apply` command with the provided YAML configuration.

## Logging Configuration

- **LOGGING_LEVEL**
- Description: Sets the logging level for application logs.
- Value: "info"

- **ROOT_LOGGING_LEVEL**
- Description: Sets the root logging level for the application.
- Value: "error"

## Database Configuration

- **dbdriver**
- Description: Database driver configuration for JDBC.
- Value: "jdbc:postgresql://"

## Cluster Configuration

- **clustertype**
- Description: Defines the cluster type.
- Value: "kubernetes"

- **k8stype**
- Description: Kubernetes type configuration.
- Value: "openshift"

- **authtype**
- Description: Authentication type for the cluster.
- Value: "openshift"

## Monitoring Configuration

- **monitoringagent**
- Description: Specifies the monitoring agent used.
- Value: "prometheus"

- **monitoringservice**
- Description: Monitoring service configuration.
- Value: "prometheus-k8s"

- **monitoringendpoint**
- Description: Monitoring endpoint configuration.
- Value: "prometheus-k8s"

## Database Interaction Configuration

- **savetodb**
- Description: Indicates whether to save to the database.
- Value: "true"

## Hibernate Configuration

- **hibernate_dialect**
- Description: Hibernate dialect configuration.
- Value: "org.hibernate.dialect.PostgreSQLDialect"

- **hibernate_driver**
- Description: Hibernate database driver configuration.
- Value: "org.postgresql.Driver"

- **hibernate_c3p0minsize**
- Description: Minimum size for the C3P0 connection pool.
- Value: "5"

- **hibernate_c3p0maxsize**
- Description: Maximum size for the C3P0 connection pool.
- Value: "10"


- **hibernate_c3p0timeout**
- Description: Timeout configuration for the C3P0 connection pool.
- Value: "300"

- **hibernate_c3p0maxstatements**
- Description: Maximum statements configuration for the C3P0 connection pool.
- Value: "100"

- **hibernate_hbm2ddlauto**
- Description: Hibernate DDL auto configuration.
- Value: "none"

- **hibernate_showsql**
- Description: Enable or disable showing SQL statements in the logs.
- Value: "false"

- **hibernate_timezone**
- Description: Timezone configuration for Hibernate.
- Value: "UTC"

## Other Configuration

- **deletepartitionsthreshold**
- Description: Threshold for deleting partitions.
- Value: "16"
- Details: The value represents the number of days, indicating the duration for which partitions belonging to Kruize
that are older than the specified number of days from today's date will be deleted. For example, if the value is
set to "16," Kruize will automatically delete partitions older than 16 days, helping manage and optimize storage
resources.
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,47 @@ spec:
- name: nginx-config-volume
configMap:
name: nginx-config

---
apiVersion: batch/v1
kind: CronJob
metadata:
name: kruize-delete-partition-cronjob
namespace: monitoring
spec:
schedule: "0 0 25 * *" # Run on 25th of every month at midnight
jobTemplate:
spec:
template:
spec:
containers:
- name: kruizedeletejob
image: kruize/autotune_operator:0.0.20.1_rm
imagePullPolicy: Always
volumeMounts:
- name: config-volume
mountPath: /etc/config
command:
- sh
- -c
- |
/home/autotune/app/target/bin/RetentionPartition
args: [ "" ]
env:
- name: START_AUTOTUNE
value: "false"
- name: LOGGING_LEVEL
value: "info"
- name: ROOT_LOGGING_LEVEL
value: "error"
- name: DB_CONFIG_FILE
value: "/etc/config/dbconfigjson"
- name: KRUIZE_CONFIG_FILE
value: "/etc/config/kruizeconfigjson"
- name: deletepartitionsthreshold
value: "15"
volumes:
- name: config-volume
configMap:
name: kruizeconfig
restartPolicy: OnFailure
---
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,49 @@ spec:
name: kruizeconfig
restartPolicy: OnFailure
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: kruize-delete-partition-cronjob
namespace: openshift-tuning
spec:
schedule: "0 0 25 * *" # Run on 25th of every month at midnight
jobTemplate:
spec:
template:
spec:
containers:
- name: kruizedeletejob
image: kruize/autotune_operator:0.0.20.1_rm
imagePullPolicy: Always
volumeMounts:
- name: config-volume
mountPath: /etc/config
command:
- sh
- -c
- |
/home/autotune/app/target/bin/RetentionPartition
args: [ "" ]
env:
- name: START_AUTOTUNE
value: "false"
- name: LOGGING_LEVEL
value: "info"
- name: ROOT_LOGGING_LEVEL
value: "error"
- name: DB_CONFIG_FILE
value: "/etc/config/dbconfigjson"
- name: KRUIZE_CONFIG_FILE
value: "/etc/config/kruizeconfigjson"
- name: deletepartitionsthreshold
value: "15"
volumes:
- name: config-volume
configMap:
name: kruizeconfig
restartPolicy: OnFailure
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
Expand Down
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@
<mainClass>com.autotune.jobs.CreatePartition</mainClass>
<name>CreatePartition</name>
</program>
<program>
<mainClass>com.autotune.jobs.RetentionPartition</mainClass>
<name>RetentionPartition</name>
</program>
</programs>
</configuration>
<executions>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/autotune/database/dao/ExperimentDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public interface ExperimentDAO {
// Get KruizeResult Record
List<KruizeResultsEntry> getKruizeResultsEntry(String experiment_name, String cluster_name, Timestamp interval_start_time, Timestamp interval_end_time) throws Exception;

void deletePartitions(int thresholdDaysCount);

public void addPartitions(String tableName, String month, String year, int dayOfTheMonth, String partitionType) throws Exception;

}
54 changes: 54 additions & 0 deletions src/main/java/com/autotune/database/dao/ExperimentDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -75,6 +78,57 @@ public ValidationOutputData addExperimentToDB(KruizeExperimentEntry kruizeExperi
return validationOutputData;
}

/**
* Deletes database partitions based on a specified threshold day count.
* <p>
* This method iterates through all Kruize tables in the database, extracts the date part
* from their names, and compares it with a cutoff date = currentDate - thresholdDaysCount. Tables with dates before the cutoff
* date are eligible for deletion, and the method executes SQL statements to drop these tables.
*
* @param thresholdDaysCount The number of days to be used as the threshold for partition deletion.
* Tables with dates older than this threshold will be deleted.
* @throws RuntimeException if any exception occurs during the deletion process. The exception
* details are logged, and the deletion process continues for other tables.
*/
@Override
public void deletePartitions(int thresholdDaysCount) {
LOGGER.info("Threshold is set to {}", thresholdDaysCount);
// Calculate the date 'daysCount' days ago
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -thresholdDaysCount);
Date cutoffDate = calendar.getTime();
String yyyyMMdd = "yyyyMMdd";
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) {
List<String> tablenames = session.createNativeQuery(SELECT_ALL_KRUIZE_TABLES).getResultList();
for (String tableName : tablenames) { // Since tableName cannot be null, there is no need to implement null handling; it can be skipped.
String datePart = null;
if (tableName.startsWith(DBConstants.TABLE_NAMES.KRUIZE_RESULTS + "_")) {
datePart = tableName.substring((DBConstants.TABLE_NAMES.KRUIZE_RESULTS + "_").length());
} else if (tableName.startsWith(DBConstants.TABLE_NAMES.KRUIZE_RECOMMENDATIONS + "_")) {
datePart = tableName.substring((DBConstants.TABLE_NAMES.KRUIZE_RECOMMENDATIONS + "_").length());
}
if (null != datePart) {
Date tableDate = new SimpleDateFormat(yyyyMMdd).parse(datePart);
// Compare the date part with the cutoffDate (cutoffDate = todaysDate - thresholdDaysCount)
if (tableDate.after(cutoffDate)) {
LOGGER.debug("Table not eligible for deletion: " + tableName);
} else {
LOGGER.debug("Table found for deletion: " + tableName);
try {
Transaction tx = session.beginTransaction();
session.createNativeQuery("DROP TABLE " + tableName).executeUpdate();
tx.commit();
} catch (Exception ignored) {
LOGGER.error("Exception occurred while deleting the partition: {}", ignored.getMessage());
}
}
}
}
} catch (Exception e) {
LOGGER.error("Exception occurred while deleting the partition: {}", e.getMessage());
}
}

@Override
public void addPartitions(String tableName, String month, String year, int dayOfTheMonth, String partitionType) {
Transaction tx;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/autotune/database/helper/DBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static final class SQLQUERY {
public static final String DELETE_FROM_RESULTS_BY_EXP_NAME = "DELETE FROM KruizeResultsEntry k WHERE k.experiment_name = :experimentName";
public static final String DELETE_FROM_RECOMMENDATIONS_BY_EXP_NAME = "DELETE FROM KruizeRecommendationEntry k WHERE k.experiment_name = :experimentName";
public static final String DB_PARTITION_DATERANGE = "CREATE TABLE IF NOT EXISTS %s_%s%s%s PARTITION OF %s FOR VALUES FROM ('%s-%s-%s 00:00:00.000') TO ('%s-%s-%s 23:59:59');";
public static final String SELECT_ALL_KRUIZE_TABLES = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' " +
"and (table_name like 'kruize_results_%' or table_name like 'kruize_recommendations_%') ";
}

public static final class TABLE_NAMES {
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/com/autotune/jobs/RetentionPartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import com.autotune.analyzer.exceptions.K8sTypeNotSupportedException;
import com.autotune.analyzer.exceptions.MonitoringAgentNotFoundException;
import com.autotune.analyzer.exceptions.MonitoringAgentNotSupportedException;
import com.autotune.database.init.KruizeHibernateUtil;
import com.autotune.database.dao.ExperimentDAOImpl;
import com.autotune.operator.InitializeDeployment;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.autotune.operator.KruizeDeploymentInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -15,19 +14,14 @@ public class RetentionPartition {
private static final Logger LOGGER = LoggerFactory.getLogger(RetentionPartition.class);

public static void main(String[] args) {
LOGGER.info("Checking Liveliness probe DB connection...");
LOGGER.info("RetentionPartition");
try {
InitializeDeployment.setup_deployment_info();
new ExperimentDAOImpl().deletePartitions(KruizeDeploymentInfo.delete_partition_threshold_in_days);
} catch (Exception | K8sTypeNotSupportedException | MonitoringAgentNotSupportedException |
MonitoringAgentNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
SessionFactory factory = KruizeHibernateUtil.getSessionFactory();

Session session = factory.openSession();

session.close();
LOGGER.info("DB Liveliness probe connection successful!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Hashtable;

import static com.autotune.analyzer.utils.AnalyzerConstants.AutotuneConfigConstants.*;
import static com.autotune.utils.KruizeConstants.KRUIZE_CONFIG_DEFAULT_VALUE.DELETE_PARTITION_THRESHOLD_IN_DAYS;

/**
* Contains information about the current deployment by parsing the autotune config map
Expand Down Expand Up @@ -68,7 +69,7 @@ public class KruizeDeploymentInfo {
public static Integer bulk_update_results_limit = 100;

public static int generate_recommendations_date_range_limit_in_days = 15;

public static Integer delete_partition_threshold_in_days = DELETE_PARTITION_THRESHOLD_IN_DAYS;
private static Hashtable<String, Class> tunableLayerPair;
//private static KubernetesClient kubernetesClient;
private static KubeEventLogger kubeEventLogger;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/autotune/utils/KruizeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ public static final class KRUIZE_CONFIG_ENV_NAME {
public static final String AUTOTUNE_MODE = "autotunemode";
public static final String EM_ONLY_MODE = "emonly";
public static final String BULK_UPDATE_RESULTS_LIMIT = "bulkresultslimit";
public static final String DELETE_PARTITION_THRESHOLD_IN_DAYS = "deletepartitionsthreshold";
public static final String SETTINGS_SAVE_TO_DB = "savetodb";
public static final String SETTINGS_DB_DRIVER = "dbdriver";
public static final String SETTINGS_HIBERNATE_DIALECT = "hibernate_dialect";
Expand Down Expand Up @@ -478,4 +479,8 @@ private RecommendationDurationRanges() {


}

public static final class KRUIZE_CONFIG_DEFAULT_VALUE {
public static final int DELETE_PARTITION_THRESHOLD_IN_DAYS = 16;
}
}

0 comments on commit 5d64285

Please sign in to comment.