Skip to content

Commit af54139

Browse files
bhanvimenghanibharathappali
authored andcommitted
adds loads layers functionality
1 parent 8838721 commit af54139

File tree

6 files changed

+170
-2
lines changed

6 files changed

+170
-2
lines changed

src/main/java/com/autotune/database/dao/ExperimentDAO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public interface ExperimentDAO {
4949
// Add Layer to DB
5050
public ValidationOutputData addLayerToDB(KruizeLMLayerEntry kruizeLayerEntry);
5151

52+
// If Kruize restarts load all layers
53+
List<KruizeLMLayerEntry> loadAllLayers() throws Exception;
54+
55+
// Load a single Layer based on layer name
56+
List<KruizeLMLayerEntry> loadLayerByName(String layerName) throws Exception;
57+
5258
// Add DataSource to DB
5359
ValidationOutputData addDataSourceToDB(KruizeDataSourceEntry kruizeDataSourceEntry, ValidationOutputData validationOutputData);
5460

src/main/java/com/autotune/database/dao/ExperimentDAOImpl.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,62 @@ public List<KruizeLMMetadataProfileEntry> loadAllMetadataProfiles() throws Excep
14341434
return entries;
14351435
}
14361436

1437+
/**
1438+
* Fetches all the Layer records from KruizeLMLayerEntry database table
1439+
*
1440+
* @return List of all KruizeLMLayerEntry database objects
1441+
* @throws Exception
1442+
*/
1443+
@Override
1444+
public List<KruizeLMLayerEntry> loadAllLayers() throws Exception {
1445+
String statusValue = "failure";
1446+
Timer.Sample timerLoadAllLayers = Timer.start(MetricsConfig.meterRegistry());
1447+
1448+
List<KruizeLMLayerEntry> entries = null;
1449+
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) {
1450+
entries = session.createQuery(DBConstants.SQLQUERY.SELECT_FROM_LAYER, KruizeLMLayerEntry.class).list();
1451+
statusValue = "success";
1452+
} catch (Exception e) {
1453+
LOGGER.error("Not able to load Layers due to {}", e.getMessage());
1454+
throw new Exception("Error while loading existing Layers from database due to : " + e.getMessage());
1455+
} finally {
1456+
if (null != timerLoadAllLayers) {
1457+
MetricsConfig.timerLoadAllLayers = MetricsConfig.timerBLoadAllLayers.tag("status", statusValue).register(MetricsConfig.meterRegistry());
1458+
timerLoadAllLayers.stop(MetricsConfig.timerLoadAllLayers);
1459+
}
1460+
}
1461+
return entries;
1462+
}
1463+
1464+
/**
1465+
* Fetches a single Layer record by layer name from KruizeLMLayerEntry database table
1466+
*
1467+
* @param layerName The name of the layer to retrieve
1468+
* @return List of KruizeLMLayerEntry database objects (should contain 0 or 1 element)
1469+
* @throws Exception
1470+
*/
1471+
@Override
1472+
public List<KruizeLMLayerEntry> loadLayerByName(String layerName) throws Exception {
1473+
String statusValue = "failure";
1474+
Timer.Sample timerLoadLayerByName = Timer.start(MetricsConfig.meterRegistry());
1475+
1476+
List<KruizeLMLayerEntry> entries = null;
1477+
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) {
1478+
entries = session.createQuery(DBConstants.SQLQUERY.SELECT_FROM_LAYER_BY_NAME, KruizeLMLayerEntry.class)
1479+
.setParameter("layerName", layerName).list();
1480+
statusValue = "success";
1481+
} catch (Exception e) {
1482+
LOGGER.error("Not able to load Layer {} due to {}", layerName, e.getMessage());
1483+
throw new Exception("Error while loading Layer from database due to : " + e.getMessage());
1484+
} finally {
1485+
if (null != timerLoadLayerByName) {
1486+
MetricsConfig.timerLoadLayerByName = MetricsConfig.timerBLoadLayerByName.tag("status", statusValue).register(MetricsConfig.meterRegistry());
1487+
timerLoadLayerByName.stop(MetricsConfig.timerLoadLayerByName);
1488+
}
1489+
}
1490+
return entries;
1491+
}
1492+
14371493
@Override
14381494
public List<KruizeLMExperimentEntry> loadLMExperimentByName(String experimentName) throws Exception {
14391495
//todo load only experimentStatus=inprogress , playback may not require completed experiments

src/main/java/com/autotune/database/helper/DBConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ public static final class SQLQUERY {
8585
public static final String SELECT_FROM_METRIC_PROFILE_BY_NAME = "from KruizeMetricProfileEntry k WHERE k.name = :name";
8686
public static final String SELECT_FROM_METADATA_PROFILE = "from KruizeLMMetadataProfileEntry";
8787
public static final String SELECT_FROM_METADATA_PROFILE_BY_NAME = "from KruizeLMMetadataProfileEntry k WHERE k.name = :name";
88+
public static final String SELECT_FROM_LAYER = "from KruizeLMLayerEntry";
89+
public static final String SELECT_FROM_LAYER_BY_NAME = "from KruizeLMLayerEntry k WHERE k.layer_name = :layerName";
8890
public static final String DELETE_FROM_EXPERIMENTS_BY_EXP_NAME = "DELETE FROM KruizeExperimentEntry k WHERE k.experiment_name = :experimentName";
8991
public static final String DELETE_FROM_LM_EXPERIMENTS_BY_EXP_NAME = "DELETE FROM KruizeLMExperimentEntry k WHERE k.experiment_name = :experimentName";
9092
public static final String DELETE_FROM_RESULTS_BY_EXP_NAME = "DELETE FROM KruizeResultsEntry k WHERE k.experiment_name = :experimentName";

src/main/java/com/autotune/database/helper/DBHelpers.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.autotune.analyzer.adapters.MetricMetadataAdapter;
2121
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
2222
import com.autotune.analyzer.exceptions.InvalidConversionOfRecommendationEntryException;
23+
import com.autotune.analyzer.kruizeLayer.KruizeLayer;
2324
import com.autotune.analyzer.kruizeObject.KruizeObject;
2425
import com.autotune.analyzer.kruizeObject.SloInfo;
2526
import com.autotune.analyzer.metadataProfiles.MetadataProfile;
@@ -1605,6 +1606,91 @@ public static KruizeLMLayerEntry convertLayerObjectToLayerDBObj(com.autotune.ana
16051606
throw new Exception("Error converting KruizeLayer to database object: " + e.getMessage(), e);
16061607
}
16071608
}
1609+
1610+
/**
1611+
* Convert KruizeLMLayerEntry database object to KruizeLayer object
1612+
*
1613+
* @param entry KruizeLMLayerEntry database entry object to be converted
1614+
* @return KruizeLayer domain object
1615+
*/
1616+
public static KruizeLayer convertLayerDBObjToLayerObject(KruizeLMLayerEntry entry) {
1617+
KruizeLayer kruizeLayer = null;
1618+
try {
1619+
kruizeLayer = new KruizeLayer();
1620+
kruizeLayer.setApiVersion(entry.getApi_version());
1621+
kruizeLayer.setKind(entry.getKind());
1622+
kruizeLayer.setLayerName(entry.getLayer_name());
1623+
kruizeLayer.setLayerLevel(entry.getLayer_level());
1624+
kruizeLayer.setDetails(entry.getDetails());
1625+
1626+
Gson gson = new Gson();
1627+
1628+
// Convert JsonNode to metadata object
1629+
if (entry.getMetadata() != null) {
1630+
try {
1631+
String metadataJson = entry.getMetadata().toString();
1632+
com.autotune.analyzer.kruizeLayer.LayerMetadata metadata = gson.fromJson(metadataJson, com.autotune.analyzer.kruizeLayer.LayerMetadata.class);
1633+
kruizeLayer.setMetadata(metadata);
1634+
} catch (Exception e) {
1635+
throw new Exception("Error processing layer metadata: " + e.getMessage());
1636+
}
1637+
}
1638+
1639+
// Convert JsonNode to layer_presence object
1640+
if (entry.getLayer_presence() != null) {
1641+
try {
1642+
String layerPresenceJson = entry.getLayer_presence().toString();
1643+
com.autotune.analyzer.kruizeLayer.LayerPresence layerPresence = gson.fromJson(layerPresenceJson, com.autotune.analyzer.kruizeLayer.LayerPresence.class);
1644+
kruizeLayer.setLayerPresence(layerPresence);
1645+
} catch (Exception e) {
1646+
throw new Exception("Error processing layer presence: " + e.getMessage());
1647+
}
1648+
}
1649+
1650+
// Convert JsonNode to tunables list
1651+
if (entry.getTunables() != null) {
1652+
try {
1653+
String tunablesJson = entry.getTunables().toString();
1654+
com.autotune.analyzer.kruizeLayer.Tunable[] tunablesArray = gson.fromJson(tunablesJson, com.autotune.analyzer.kruizeLayer.Tunable[].class);
1655+
ArrayList<com.autotune.analyzer.kruizeLayer.Tunable> tunablesList = new ArrayList<>(java.util.Arrays.asList(tunablesArray));
1656+
kruizeLayer.setTunables(tunablesList);
1657+
} catch (Exception e) {
1658+
throw new Exception("Error processing layer tunables: " + e.getMessage());
1659+
}
1660+
}
1661+
} catch (Exception e) {
1662+
LOGGER.error("Failed to convert KruizeLMLayerEntry to KruizeLayer object: {}", e.getMessage());
1663+
e.printStackTrace();
1664+
}
1665+
return kruizeLayer;
1666+
}
1667+
1668+
/**
1669+
* Converts List of KruizeLMLayerEntry DB objects to List of KruizeLayer objects
1670+
*
1671+
* @param kruizeLayerEntryList List of KruizeLMLayerEntry database objects to be converted
1672+
* @return List of KruizeLayer domain objects
1673+
* @throws Exception
1674+
*/
1675+
public static List<KruizeLayer> convertLayerEntryToLayerObject(List<KruizeLMLayerEntry> kruizeLayerEntryList) throws Exception {
1676+
List<KruizeLayer> kruizeLayerList = new ArrayList<>();
1677+
try {
1678+
for (KruizeLMLayerEntry entry : kruizeLayerEntryList) {
1679+
try {
1680+
KruizeLayer kruizeLayer = convertLayerDBObjToLayerObject(entry);
1681+
if (kruizeLayer != null) {
1682+
kruizeLayerList.add(kruizeLayer);
1683+
}
1684+
} catch (Exception e) {
1685+
LOGGER.error("Error occurred while converting layer entry to layer object: {}", e.getMessage());
1686+
}
1687+
}
1688+
} catch (Exception e) {
1689+
LOGGER.error("Error while converting layer entries to layer objects: {}", e.getMessage());
1690+
throw new Exception("Error while converting layer entries to layer objects: " + e.getMessage());
1691+
}
1692+
return kruizeLayerList;
1693+
}
16081694
}
16091695

16101696
}

src/main/java/com/autotune/database/service/ExperimentDBService.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,22 @@ public ValidationOutputData addLayerToDB(KruizeLayer kruizeLayer) {
483483
return validationOutputData;
484484
}
485485

486+
/**
487+
* Load all layers from the database and populate them into the provided map
488+
*
489+
* @param layerMap Map to store the loaded layers (key: layer_name, value: KruizeLayer)
490+
* @throws Exception if there's an error loading or converting layers
491+
*/
492+
public void loadAllLayers(Map<String, KruizeLayer> layerMap) throws Exception {
493+
List<KruizeLMLayerEntry> entries = experimentDAO.loadAllLayers();
494+
if (null != entries && !entries.isEmpty()) {
495+
List<KruizeLayer> kruizeLayers = DBHelpers.Converters.KruizeObjectConverters.convertLayerEntryToLayerObject(entries);
496+
if (!kruizeLayers.isEmpty()) {
497+
kruizeLayers.forEach(layer -> layerMap.put(layer.getLayerName(), layer));
498+
}
499+
}
500+
}
501+
486502

487503
/*
488504
* This is a Java method that loads all experiments from the database using an experimentDAO object.

src/main/java/com/autotune/utils/MetricsConfig.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class MetricsConfig {
2222
public static Timer timerAddRecDB, timerAddResultsDB, timerAddExpDB, timerAddBulkResultsDB, timerSaveBulkJobDB, timerAddBulkJob;
2323
public static Timer timerAddPerfProfileDB, timerLoadPerfProfileName, timerLoadAllPerfProfiles;
2424
public static Timer timerAddMetadataProfileDB, timerLoadMetadataProfileName, timerLoadAllMetadataProfiles, timerUpdateMetadataProfileDB;
25-
public static Timer timerAddLayerDB;
25+
public static Timer timerAddLayerDB, timerLoadAllLayers, timerLoadLayerByName;
2626
public static Timer timerImportMetadata, timerGetMetadata;
2727
public static Timer timerJobStatus, timerCreateBulkJob, timerGetExpMap, timerCreateBulkExp, timerGenerateBulkRec, timerRunJob;
2828
public static Counter timerKruizeNotifications , timerBulkJobs;
@@ -42,7 +42,7 @@ public class MetricsConfig {
4242
public static Timer.Builder timerBUpdateMetadataProfile;
4343
public static Timer timerUpdatePerfProfile;
4444
public static Timer.Builder timerBUpdatePerfProfile;
45-
public static Timer.Builder timerBAddLayerDB;
45+
public static Timer.Builder timerBAddLayerDB, timerBLoadAllLayers, timerBLoadLayerByName;
4646

4747
private static MetricsConfig INSTANCE;
4848
public String API_METRIC_DESC = "Time taken for Kruize APIs";
@@ -107,6 +107,8 @@ private MetricsConfig() {
107107
timerBUpdatePerfProfileDB = Timer.builder("kruizeDB").description(DB_METRIC_DESC).tag("method", "updatePerformanceProfileInDB");
108108
timerBUpdatePerfProfile = Timer.builder("kruizeAPI").description(API_METRIC_DESC).tag("api", "updatePerformanceProfile").tag("method", "PUT");
109109
timerBAddLayerDB = Timer.builder("kruizeDB").description(DB_METRIC_DESC).tag("method", "addLayerToDB");
110+
timerBLoadAllLayers = Timer.builder("kruizeDB").description(DB_METRIC_DESC).tag("method", "loadAllLayers");
111+
timerBLoadLayerByName = Timer.builder("kruizeDB").description(DB_METRIC_DESC).tag("method", "loadLayerByName");
110112

111113
new ClassLoaderMetrics().bindTo(meterRegistry);
112114
new ProcessorMetrics().bindTo(meterRegistry);

0 commit comments

Comments
 (0)