Skip to content
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

[Enhancement] Support Gson subtype rollback #50471

Merged
merged 2 commits into from
Sep 19, 2024

Conversation

gengjun-git
Copy link
Contributor

@gengjun-git gengjun-git commented Aug 30, 2024

Why I'm doing:

What I'm doing:

SubtypeNotFoundException will be thrown when there is a undefined class when deserialize a subclass. The readCollection and readMapEntry will ignore the SubtypeNotFoundException if ignore_unknown_subtype is true.

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

return (T) object.getValue();
}
return GsonUtils.GSON.fromJson(jsonReader, typeOfT);
}

@Override
public void close() throws IOException {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
The casting in the readJson method may lead to a ClassCastException.

You can modify the code like this:

@Override
public <T> T readJson(Class<T> classOfT) throws IOException, SRMetaBlockEOFException {
    Object object = readJson((Type) classOfT);
    if(!Primitives.wrap(classOfT).isInstance(object)) {
        throw new JsonSyntaxException("Expected type " + classOfT.getName() + " but got " 
                                      + (object == null ? "null" : object.getClass().getName()));
    }
    return Primitives.wrap(classOfT).cast(object);
}

}
}
}

@Override
public void close() throws IOException, SRMetaBlockException {
if (header == null) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
Potential ClassCastException in the readJson(Class<T> classOfT) method.

You can modify the code like this:

@Override
public <T> T readJson(Class<T> classOfT) throws IOException, SRMetaBlockEOFException {
    T object = GsonUtils.GSON.fromJson(genJsonReader(readJsonBytes()), classOfT);
    return Primitives.wrap(classOfT).cast(object);
}


// Initialize the Authorizer class in advance during the loading phase
// to prevent loading errors and lack of permissions.
Authorizer.getInstance();
}

public void saveV2(ImageWriter imageWriter) throws IOException {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most risky bug in this code is:
Potentially throwing IOException within the lambda functions of readMapEntry, which may not be adequately handled and could be suppressed.

You can modify the code like this:

public void loadV2(SRMetaBlockReader reader) throws IOException, SRMetaBlockException, SRMetaBlockEOFException {
    // 1 json for myself
    AuthorizationMgr ret = reader.readJson(AuthorizationMgr.class);
    ret.globalStateMgr = globalStateMgr;
    ret.provider = Objects.requireNonNullElseGet(provider, DefaultAuthorizationProvider::new);
    ret.initBuiltinRolesAndUsers();

    try {
        LOG.info("loading users");
        reader.readMapEntry(UserIdentity.class, UserPrivilegeCollectionV2.class,
                (MapEntryConsumer<UserIdentity, UserPrivilegeCollectionV2>) (userIdentity, collection) -> {
                    if (userIdentity.equals(UserIdentity.ROOT)) {
                        try {
                            UserPrivilegeCollectionV2 rootUserPrivCollection =
                                    ret.getUserPrivilegeCollectionUnlocked(UserIdentity.ROOT);
                            collection.grantRoles(rootUserPrivCollection.getAllRoles());
                            collection.setDefaultRoleIds(rootUserPrivCollection.getDefaultRoleIds());
                            collection.typeToPrivilegeEntryList = rootUserPrivCollection.typeToPrivilegeEntryList;
                        } catch (PrivilegeException e) {
                            throw new RuntimeException("failed to load users in AuthorizationManager!", e);
                        }
                    }
                    ret.userToPrivilegeCollection.put(userIdentity, collection);
                });

        LOG.info("loading roles");
        reader.readMapEntry(Long.class, RolePrivilegeCollectionV2.class,
                (MapEntryConsumer<Long, RolePrivilegeCollectionV2>) (roleId, collection) -> {
                    // Use hard-code PrivilegeCollection in the memory as the built-in role permission.
                    // The reason why need to replay from the image here
                    // is because the associated information of the role-id is stored in the image.
                    if (PrivilegeBuiltinConstants.IMMUTABLE_BUILT_IN_ROLE_IDS.contains(roleId)) {
                        RolePrivilegeCollectionV2 builtInRolePrivilegeCollection =
                                ret.roleIdToPrivilegeCollection.get(roleId);
                        collection.typeToPrivilegeEntryList = builtInRolePrivilegeCollection.typeToPrivilegeEntryList;
                    }
                    ret.roleIdToPrivilegeCollection.put(roleId, collection);
                });

        LOG.info("loaded {} users, {} roles",
                ret.userToPrivilegeCollection.size(), ret.roleIdToPrivilegeCollection.size());

        // mark data is loaded
        isLoaded = true;
        roleNameToId = ret.roleNameToId;
        pluginId = ret.pluginId;
        pluginVersion = ret.pluginVersion;
        userToPrivilegeCollection = ret.userToPrivilegeCollection;
        roleIdToPrivilegeCollection = ret.roleIdToPrivilegeCollection;

        // Initialize the Authorizer class in advance during the loading phase
        // to prevent loading errors and lack of permissions.
        Authorizer.getInstance();
    } catch (RuntimeException e) {
        throw new IOException("Failed to load AuthorizationManager!", e);
    }
}

@nshangyiming nshangyiming self-assigned this Sep 4, 2024
@gengjun-git
Copy link
Contributor Author

@Mergifyio rebase

Copy link
Contributor

mergify bot commented Sep 5, 2024

rebase

✅ Branch has been successfully rebased

kevincai
kevincai previously approved these changes Sep 5, 2024
@wyb
Copy link
Contributor

wyb commented Sep 5, 2024

https://github.com/StarRocks/starrocks/blob/main/fe/fe-core/src/main/java/com/starrocks/server/GlobalStateMgr.java#L1529

unknown meta block is ignored by default if rollback to old version, is this by design?

@gengjun-git
Copy link
Contributor Author

https://github.com/StarRocks/starrocks/blob/main/fe/fe-core/src/main/java/com/starrocks/server/GlobalStateMgr.java#L1529

unknown meta block is ignored by default if rollback to old version, is this by design?

yes, ignore the unknown meta data block when rollback

@gengjun-git
Copy link
Contributor Author

@Mergifyio rebase

Copy link
Contributor

mergify bot commented Sep 9, 2024

rebase

✅ Branch has been successfully rebased

wyb
wyb previously approved these changes Sep 10, 2024
nshangyiming
nshangyiming previously approved these changes Sep 13, 2024
@gengjun-git
Copy link
Contributor Author

@Mergifyio rebase

Copy link
Contributor

mergify bot commented Sep 18, 2024

rebase

✅ Branch has been successfully rebased

Signed-off-by: gengjun-git <[email protected]>
Signed-off-by: gengjun-git <[email protected]>
Copy link

sonarcloud bot commented Sep 19, 2024

Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 266 / 314 (84.71%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/load/streamload/StreamLoadMgr.java 0 3 00.00% [659, 664, 668]
🔵 com/starrocks/catalog/CatalogRecycleBin.java 0 6 00.00% [1066, 1068, 1070, 1073, 1075, 1077]
🔵 com/starrocks/scheduler/TaskManager.java 0 2 00.00% [577, 579]
🔵 com/starrocks/load/DeleteMgr.java 0 2 00.00% [895, 896]
🔵 com/starrocks/catalog/ResourceGroupMgr.java 0 1 00.00% [682]
🔵 com/starrocks/catalog/GlobalFunctionMgr.java 0 1 00.00% [205]
🔵 com/starrocks/persist/EditLog.java 0 1 00.00% [1205]
🔵 com/starrocks/scheduler/mv/MaterializedViewMgr.java 0 2 00.00% [303, 308]
🔵 com/starrocks/plugin/PluginMgr.java 0 1 00.00% [353]
🔵 com/starrocks/server/GlobalStateMgr.java 1 3 33.33% [2033, 2034]
🔵 com/starrocks/load/ExportMgr.java 2 3 66.67% [434]
🔵 com/starrocks/load/loadv2/LoadMgr.java 2 3 66.67% [786]
🔵 com/starrocks/persist/metablock/PrimitiveObject.java 4 6 66.67% [33, 34]
🔵 com/starrocks/backup/BackupHandler.java 2 3 66.67% [708]
🔵 com/starrocks/qe/VariableMgr.java 8 12 66.67% [410, 411, 421, 422]
🔵 com/starrocks/load/routineload/RoutineLoadMgr.java 2 3 66.67% [793]
🔵 com/starrocks/alter/AlterJobMgr.java 3 4 75.00% [626]
🔵 com/starrocks/authentication/AuthenticationMgr.java 8 10 80.00% [592, 593]
🔵 com/starrocks/transaction/GlobalTransactionMgr.java 4 5 80.00% [766]
🔵 com/starrocks/persist/metablock/SRMetaBlockReaderV1.java 46 51 90.20% [196, 197, 198, 199, 201]
🔵 com/starrocks/privilege/AuthorizationMgr.java 32 34 94.12% [1708, 1709]
🔵 com/starrocks/persist/metablock/SRMetaBlockReaderV2.java 95 101 94.06% [185, 186, 187, 188, 189, 191]
🔵 com/starrocks/load/pipe/PipeRepo.java 2 2 100.00% []
🔵 com/starrocks/persist/gson/SubtypeNotFoundException.java 4 4 100.00% []
🔵 com/starrocks/persist/metablock/SRMetaBlockWriterV1.java 14 14 100.00% []
🔵 com/starrocks/statistic/AnalyzeMgr.java 6 6 100.00% []
🔵 com/starrocks/persist/metablock/SRMetaBlockWriterV2.java 16 16 100.00% []
🔵 com/starrocks/common/Config.java 2 2 100.00% []
🔵 com/starrocks/server/CatalogMgr.java 2 2 100.00% []
🔵 com/starrocks/server/LocalMetastore.java 3 3 100.00% []
🔵 com/starrocks/encryption/KeyMgr.java 3 3 100.00% []
🔵 com/starrocks/persist/gson/GsonUtils.java 2 2 100.00% []
🔵 com/starrocks/common/util/SmallFileMgr.java 1 1 100.00% []
🔵 com/starrocks/persist/gson/RuntimeTypeAdapterFactory.java 1 1 100.00% []
🔵 com/starrocks/journal/JournalEntity.java 1 1 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@gengjun-git gengjun-git enabled auto-merge (squash) September 19, 2024 09:24
@gengjun-git gengjun-git merged commit 790a2b0 into StarRocks:main Sep 19, 2024
54 checks passed
Copy link

@Mergifyio backport branch-3.3

@github-actions github-actions bot removed the 3.3 label Sep 19, 2024
Copy link
Contributor

mergify bot commented Sep 19, 2024

backport branch-3.3

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Sep 19, 2024
SubtypeNotFoundException will be thrown when there is a undefined class when deserialize a subclass. The `readCollection` and `readMapEntry` will ignore the SubtypeNotFoundException if ignore_unknown_subtype is true.

Signed-off-by: gengjun-git <[email protected]>
(cherry picked from commit 790a2b0)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/journal/JournalEntity.java
#	fe/fe-core/src/main/java/com/starrocks/qe/VariableMgr.java
#	fe/fe-core/src/test/java/com/starrocks/plugin/PluginMgrTest.java
wanpengfei-git pushed a commit that referenced this pull request Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants