kasakrisz commented on a change in pull request #2984:
URL: https://github.com/apache/hive/pull/2984#discussion_r795393883
##########
File path:
ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java
##########
@@ -139,109 +200,262 @@ public void init() {
}
}
- public void init(Hive db) {
+ private static void init(Hive db) {
final boolean dummy =
db.getConf().get(HiveConf.ConfVars.HIVE_SERVER2_MATERIALIZED_VIEWS_REGISTRY_IMPL.varname)
.equals("DUMMY");
if (dummy) {
// Dummy registry does not cache information and forwards all requests
to metastore
- initialized.set(true);
+ SINGLETON = new MaterializedViewsRegistry() {};
LOG.info("Using dummy materialized views registry");
} else {
+ SINGLETON = new
InMemoryMaterializedViewsRegistry(HiveMaterializedViewsRegistry::createMaterialization);
// We initialize the cache
long period = HiveConf.getTimeVar(db.getConf(),
ConfVars.HIVE_SERVER2_MATERIALIZED_VIEWS_REGISTRY_REFRESH, TimeUnit.SECONDS);
+ if (period <= 0) {
+ return;
+ }
+
ScheduledExecutorService pool =
Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("HiveMaterializedViewsRegistry-%d")
.build());
- pool.scheduleAtFixedRate(new Loader(db), 0, period, TimeUnit.SECONDS);
+
+ MaterializedViewObjects objects =
db::getAllMaterializedViewObjectsForRewriting;
+ pool.scheduleAtFixedRate(new Loader(db.getConf(), SINGLETON, objects),
0, period, TimeUnit.SECONDS);
}
}
- private class Loader implements Runnable {
- private final Hive db;
+ public interface MaterializedViewObjects {
+ List<Table> getAllMaterializedViewObjectsForRewriting() throws
HiveException;
+ }
- private Loader(Hive db) {
- this.db = db;
+ public static class Loader implements Runnable {
+ protected final HiveConf hiveConf;
+ protected final MaterializedViewsRegistry materializedViewsRegistry;
+ protected final MaterializedViewObjects materializedViewObjects;
+ /* Whether the cache has been initialized or not. */
+
+ Loader(HiveConf hiveConf,
+ MaterializedViewsRegistry materializedViewsRegistry,
+ MaterializedViewObjects materializedViewObjects) {
+ this.hiveConf = hiveConf;
+ this.materializedViewsRegistry = materializedViewsRegistry;
+ this.materializedViewObjects = materializedViewObjects;
}
@Override
public void run() {
- SessionState ss = new SessionState(db.getConf());
- ss.setIsHiveServerQuery(true); // All is served from HS2, we do not need
e.g. Tez sessions
- SessionState.start(ss);
- PerfLogger perfLogger = SessionState.getPerfLogger();
+ refresh();
+ }
+
+ public void refresh() {
+ PerfLogger perfLogger = getPerfLogger();
perfLogger.perfLogBegin(CLASS_NAME,
PerfLogger.MATERIALIZED_VIEWS_REGISTRY_REFRESH);
try {
- if (initialized.get()) {
- for (Table mvTable : db.getAllMaterializedViewObjectsForRewriting())
{
- RelOptMaterialization existingMV = getRewritingMaterializedView(
- mvTable.getDbName(), mvTable.getTableName(), ALL);
- if (existingMV != null) {
- // We replace if the existing MV is not newer
- Table existingMVTable =
HiveMaterializedViewUtils.extractTable(existingMV);
- if (existingMVTable.getCreateTime() < mvTable.getCreateTime() ||
- (existingMVTable.getCreateTime() == mvTable.getCreateTime()
&&
- existingMVTable.getMVMetadata().getMaterializationTime()
<= mvTable.getMVMetadata().getMaterializationTime())) {
- refreshMaterializedView(db.getConf(), existingMVTable,
mvTable);
- }
- } else {
- // Simply replace if it still does not exist
- refreshMaterializedView(db.getConf(), null, mvTable);
+ List<Table> materializedViewObjects =
this.materializedViewObjects.getAllMaterializedViewObjectsForRewriting();
+ for (Table mvTable : materializedViewObjects) {
+ RelOptMaterialization existingMV =
materializedViewsRegistry.getRewritingMaterializedView(
+ mvTable.getDbName(), mvTable.getTableName(), ALL);
+ if (existingMV != null) {
+ // We replace if the existing MV is not newer
+ Table existingMVTable =
HiveMaterializedViewUtils.extractTable(existingMV);
+ if (existingMVTable.getCreateTime() < mvTable.getCreateTime() ||
+ (existingMVTable.getCreateTime() ==
mvTable.getCreateTime() &&
+
existingMVTable.getMVMetadata().getMaterializationTime() <=
mvTable.getMVMetadata().getMaterializationTime())) {
+ materializedViewsRegistry.refreshMaterializedView(hiveConf,
existingMVTable, mvTable);
}
+ } else {
+ // Simply replace if it still does not exist
+ materializedViewsRegistry.refreshMaterializedView(hiveConf, null,
mvTable);
}
- LOG.info("Materialized views registry has been refreshed");
- } else {
- for (Table mvTable : db.getAllMaterializedViewObjectsForRewriting())
{
- refreshMaterializedView(db.getConf(), null, mvTable);
+ }
+
+ for (HiveRelOptMaterialization materialization :
materializedViewsRegistry.getRewritingMaterializedViews()) {
+ Table mvTableInCache =
HiveMaterializedViewUtils.extractTable(materialization);
+ Table mvTableInHMS = materializedViewObjects.stream()
Review comment:
Update is scheduled at a fixed rate:
```
pool.scheduleAtFixedRate(new Loader(db.getConf(), SINGLETON, objects), 0,
period, TimeUnit.SECONDS);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]