Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
First take - persist any Object into database not just FlowUnits
Browse files Browse the repository at this point in the history
  • Loading branch information
yojs committed Sep 1, 2020
1 parent fd06a1e commit 3221cc0
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.persistence;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AColumn {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.persistence;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ATable {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.gson.JsonElement;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -48,6 +49,9 @@ public interface Persistable {
*/
JsonElement read(String rca);

<T> T read(Class<T> object)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException;

/**
* Write data to the database.
*
Expand All @@ -56,6 +60,8 @@ public interface Persistable {
*/
<T extends ResourceFlowUnit> void write(Node<?> node, T flowUnit) throws SQLException, IOException;

<T> long write(T object) throws Exception;

void close() throws SQLException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.gson.JsonElement;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
Expand Down Expand Up @@ -188,6 +189,28 @@ public synchronized <T extends ResourceFlowUnit> void write(Node<?> node, T flow
}
}

public synchronized <T> long write(T obj) throws Exception {
rotateRegisterGarbageThenCreateNewDB(RotationType.TRY_ROTATE);
try {
return writeImpl(obj);
} catch (IllegalStateException | IllegalArgumentException illegalEx) {
throw illegalEx;
}
catch (Exception e) {
LOG.info("RCA: Fail to write.", e);
rotateRegisterGarbageThenCreateNewDB(RotationType.FORCE_ROTATE);
try {
return writeImpl(obj);
} catch (Exception ex) {
LOG.error("Failed to write multiple times. Giving up.");
// We rethrow this exception so that framework can take appropriate action.
throw e;
}
}
}

abstract <T> long writeImpl(T obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException;

private synchronized void rotateRegisterGarbageThenCreateNewDB(RotationType type) throws IOException, SQLException {
Path rotatedFile = null;
long currTime = System.currentTimeMillis();
Expand Down
Loading

0 comments on commit 3221cc0

Please sign in to comment.