-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#3854] feat(core): Add a cache service to cache the mapping of name identifer to id #3986
base: main
Are you sure you want to change the base?
Changes from 17 commits
2be3605
18c84fc
b86081f
732c3b4
ee46442
9efd143
23edf70
36d420a
15378fb
f70e1ad
ba965f0
93989e9
9afab5c
57fae17
1d13217
593d84e
141969a
5de00b7
7903c9b
fff3ed7
efb7b55
2ee6143
b46a1e7
4253fd2
cdc5821
a5fcec2
eda1da5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
import org.apache.gravitino.Config; | ||
import org.apache.gravitino.Configs; | ||
import org.apache.gravitino.Entity; | ||
import org.apache.gravitino.Entity.EntityType; | ||
import org.apache.gravitino.EntityAlreadyExistsException; | ||
import org.apache.gravitino.HasIdentifier; | ||
import org.apache.gravitino.MetadataObject; | ||
|
@@ -54,6 +55,8 @@ | |
import org.apache.gravitino.storage.relational.service.FilesetMetaService; | ||
import org.apache.gravitino.storage.relational.service.GroupMetaService; | ||
import org.apache.gravitino.storage.relational.service.MetalakeMetaService; | ||
import org.apache.gravitino.storage.relational.service.NameIdMappingService; | ||
import org.apache.gravitino.storage.relational.service.NameIdMappingService.EntityIdentifier; | ||
import org.apache.gravitino.storage.relational.service.OwnerMetaService; | ||
import org.apache.gravitino.storage.relational.service.RoleMetaService; | ||
import org.apache.gravitino.storage.relational.service.SchemaMetaService; | ||
|
@@ -123,36 +126,54 @@ public boolean exists(NameIdentifier ident, Entity.EntityType entityType) throws | |
@Override | ||
public <E extends Entity & HasIdentifier> void insert(E e, boolean overwritten) | ||
throws EntityAlreadyExistsException, IOException { | ||
EntityType entityType; | ||
if (e instanceof BaseMetalake) { | ||
MetalakeMetaService.getInstance().insertMetalake((BaseMetalake) e, overwritten); | ||
entityType = EntityType.METALAKE; | ||
} else if (e instanceof CatalogEntity) { | ||
CatalogMetaService.getInstance().insertCatalog((CatalogEntity) e, overwritten); | ||
entityType = EntityType.CATALOG; | ||
} else if (e instanceof SchemaEntity) { | ||
SchemaMetaService.getInstance().insertSchema((SchemaEntity) e, overwritten); | ||
entityType = EntityType.SCHEMA; | ||
} else if (e instanceof TableEntity) { | ||
TableMetaService.getInstance().insertTable((TableEntity) e, overwritten); | ||
entityType = EntityType.TABLE; | ||
} else if (e instanceof FilesetEntity) { | ||
FilesetMetaService.getInstance().insertFileset((FilesetEntity) e, overwritten); | ||
entityType = EntityType.FILESET; | ||
} else if (e instanceof TopicEntity) { | ||
TopicMetaService.getInstance().insertTopic((TopicEntity) e, overwritten); | ||
entityType = EntityType.TOPIC; | ||
} else if (e instanceof UserEntity) { | ||
UserMetaService.getInstance().insertUser((UserEntity) e, overwritten); | ||
entityType = EntityType.USER; | ||
} else if (e instanceof RoleEntity) { | ||
RoleMetaService.getInstance().insertRole((RoleEntity) e, overwritten); | ||
entityType = EntityType.ROLE; | ||
} else if (e instanceof GroupEntity) { | ||
GroupMetaService.getInstance().insertGroup((GroupEntity) e, overwritten); | ||
entityType = EntityType.GROUP; | ||
} else if (e instanceof TagEntity) { | ||
TagMetaService.getInstance().insertTag((TagEntity) e, overwritten); | ||
entityType = EntityType.TAG; | ||
} else { | ||
throw new UnsupportedEntityTypeException( | ||
"Unsupported entity type: %s for insert operation", e.getClass()); | ||
} | ||
|
||
NameIdMappingService.getInstance() | ||
.put(EntityIdentifier.of(e.nameIdentifier(), entityType), e.id()); | ||
} | ||
|
||
@Override | ||
public <E extends Entity & HasIdentifier> E update( | ||
NameIdentifier ident, Entity.EntityType entityType, Function<E, E> updater) | ||
throws IOException, NoSuchEntityException, EntityAlreadyExistsException { | ||
// Remove all the children entities in the cache as we can't guarantee the children entities | ||
// are still valid after the parent entity is updated. | ||
EntityIdentifier entityIdentifier = EntityIdentifier.of(ident, entityType); | ||
NameIdMappingService.getInstance().invalidateWithPrefix(entityIdentifier); | ||
switch (entityType) { | ||
case METALAKE: | ||
return (E) MetalakeMetaService.getInstance().updateMetalake(ident, updater); | ||
|
@@ -210,32 +231,45 @@ public <E extends Entity & HasIdentifier> E get( | |
} | ||
|
||
@Override | ||
public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolean cascade) | ||
throws IOException { | ||
switch (entityType) { | ||
case METALAKE: | ||
return MetalakeMetaService.getInstance().deleteMetalake(ident, cascade); | ||
case CATALOG: | ||
return CatalogMetaService.getInstance().deleteCatalog(ident, cascade); | ||
case SCHEMA: | ||
return SchemaMetaService.getInstance().deleteSchema(ident, cascade); | ||
case TABLE: | ||
return TableMetaService.getInstance().deleteTable(ident); | ||
case FILESET: | ||
return FilesetMetaService.getInstance().deleteFileset(ident); | ||
case TOPIC: | ||
return TopicMetaService.getInstance().deleteTopic(ident); | ||
case USER: | ||
return UserMetaService.getInstance().deleteUser(ident); | ||
case GROUP: | ||
return GroupMetaService.getInstance().deleteGroup(ident); | ||
case ROLE: | ||
return RoleMetaService.getInstance().deleteRole(ident); | ||
case TAG: | ||
return TagMetaService.getInstance().deleteTag(ident); | ||
default: | ||
throw new UnsupportedEntityTypeException( | ||
"Unsupported entity type: %s for delete operation", entityType); | ||
public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolean cascade) { | ||
// Invalidate the cache first | ||
EntityIdentifier entityIdentifier = EntityIdentifier.of(ident, entityType); | ||
NameIdMappingService.getInstance().invalidate(entityIdentifier); | ||
if (cascade) { | ||
// Remove all the children entities in the cache; | ||
NameIdMappingService.getInstance().invalidateWithPrefix(entityIdentifier); | ||
} | ||
|
||
try { | ||
switch (entityType) { | ||
case METALAKE: | ||
return MetalakeMetaService.getInstance().deleteMetalake(ident, cascade); | ||
case CATALOG: | ||
return CatalogMetaService.getInstance().deleteCatalog(ident, cascade); | ||
case SCHEMA: | ||
return SchemaMetaService.getInstance().deleteSchema(ident, cascade); | ||
case TABLE: | ||
return TableMetaService.getInstance().deleteTable(ident); | ||
case FILESET: | ||
return FilesetMetaService.getInstance().deleteFileset(ident); | ||
case TOPIC: | ||
return TopicMetaService.getInstance().deleteTopic(ident); | ||
case USER: | ||
return UserMetaService.getInstance().deleteUser(ident); | ||
case GROUP: | ||
return GroupMetaService.getInstance().deleteGroup(ident); | ||
case ROLE: | ||
return RoleMetaService.getInstance().deleteRole(ident); | ||
case TAG: | ||
return TagMetaService.getInstance().deleteTag(ident); | ||
default: | ||
throw new UnsupportedEntityTypeException( | ||
"Unsupported entity type: %s for delete operation", entityType); | ||
} | ||
} finally { | ||
// Remove the entity from the cache again because we may add the cache during the deletion | ||
// process | ||
NameIdMappingService.getInstance().invalidateWithPrefix(entityIdentifier); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should follow the pattern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you provide more details for the sentence |
||
} | ||
} | ||
|
||
|
@@ -327,6 +361,8 @@ public int deleteOldVersionData(Entity.EntityType entityType, long versionRetent | |
public void close() throws IOException { | ||
SqlSessionFactoryHelper.getInstance().close(); | ||
|
||
NameIdMappingService.getInstance().close(); | ||
|
||
if (jdbcDatabase != null) { | ||
jdbcDatabase.close(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EntityType entityType = e.getType();