Apache CloudStack适配GaussDB

举报
韦文满 发表于 2024/12/11 16:23:58 2024/12/11
【摘要】 Apache CloudStack是一个开源的云计算管理平台‌,旨在构建和管理私有云、公共云及混合云环境‌。CloudStack使用关系型数据库存储任务信息,官网目前只提供HikariCP的实现。本任务的主要目的是让CloudStack社区能够支持多种数据库,并且基于GaussDB数据库完成功能验证。

CloudStack简介

Apache CloudStack是一个开源的云计算管理平台‌,旨在构建和管理私有云、公共云及混合云环境‌。CloudStack使用关系型数据库存储任务信息,官网目前只提供HikariCP的实现。

GaussDB简介

GaussDB是华为公司倾力打造的自研企业级分布式关系型数据库,该产品支持优异的分布式事务,同城跨AZ部署,数据0丢失,支持1000+扩展能力,PB级海量存储等企业级数据库特性。拥有云上高可用,高可靠,高安全,弹性伸缩,一键部署,快速备份恢复,监控告警等关键能力,能为企业提供功能全面,稳定可靠,扩展性强,性能优越的企业级数据库服务。

本文目的

本文主要目的是让CloudStack社区能够支持GaussDB数据库,并且基于GaussDB数据库完成功能验证。

参考文档

前期准备工作

  1. 前往cloudstack官网,fork代码,然后git clone项目本地
wmwei@DESKTOP-7490 MINGW64 /d/huawei/work/code
$ git clone git@github.com:weiwenman/cloudstack.git
  1. 根据README初始化项目
wmwei@DESKTOP-7490 MINGW64 /d/huawei/work/code/cloudstack (main)
$ mvn clean install -P developer,systemvm

开始开发

  1. 切换到目录framework/db/,编辑pom.xml增加gaussdb驱动
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>3.1.4</version>
        </dependency>
        <dependency>
            <groupId>org.opengauss</groupId>
            <artifactId>opengauss-jdbc</artifactId>
            <version>5.1.0-og</version>
        </dependency>
  1. 修改TransactionLegacy.Java文件db配置
private static DataSource getDefaultHikaricpDataSource(final String database) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:postgresql://localhost:8000/" + database);
        config.setUsername("cloud");
        config.setPassword("cloud");
        config.setPoolName(database);
        config.setSchema("public");
        config.setDriverClassName("org.postgresql.Driver");
        config.setMaximumPoolSize(250);
        config.setConnectionTimeout(1000);
        config.setIdleTimeout(1000);
        config.setKeepaliveTime(1000);
        config.setMaxLifetime(1000);
        config.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
        config.setInitializationFailTimeout(-1L);
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        return new HikariDataSource(config);
    }
  1. 后端服务使用conn.prepareStatement方式实行sql,可通过修改调整原先适配mysql的sql语法转为GaussDB语法即可,参考以下代码:
 public String getCurrentVersion() {
        try (Connection conn = TransactionLegacy.getStandaloneConnection();) {
            logger.debug("Checking to see if the database is at a version before it was the version table is created");

            try (
                    PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'");
                    ResultSet rs = pstmt.executeQuery();
                ) {
                if (!rs.next()) {
                    try (PreparedStatement pstmt_nics = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
                         ResultSet rs_nics = pstmt_nics.executeQuery();
                        ) {
                        if (!rs_nics.next()) {
                            try (PreparedStatement pstmt_domain = conn.prepareStatement("SELECT domain_id FROM account_vlan_map LIMIT 1"); ){
                                pstmt_domain.executeQuery();
                                return "2.1.8";
                            } catch (final SQLException e) {
                                logger.debug("Assuming the exception means domain_id is not there.");
                                logger.debug("No version table and no nics table, returning 2.1.7");
                                return "2.1.7";
                            }
                        } else {
                            try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
                                 ResultSet rs_static_nat = pstmt_static_nat.executeQuery();){
                                return "2.2.1";
                            } catch (final SQLException e) {
                                logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
                                return "2.2.2";
                            }
                        }
                    }
                }
            } catch (final SQLException e) {
                throw new CloudRuntimeException("Unable to get the current version", e);
            }

            SearchCriteria<String> sc = CurrentVersionSearch.create();

            sc.setParameters("step", Step.Complete);
            Filter filter = new Filter(VersionVO.class, "id", false, 0l, 1l);
            final List<String> upgradedVersions = customSearch(sc, filter);

            if (upgradedVersions.isEmpty()) {

                // Check if there are records in Version table
                filter = new Filter(VersionVO.class, "id", false, 0l, 1l);
                sc = CurrentVersionSearch.create();
                final List<String> vers = customSearch(sc, filter);
                if (!vers.isEmpty()) {
                    throw new CloudRuntimeException("Version table contains records for which upgrade wasn't completed");
                }

                // Use nics table information and is_static_nat field from firewall_rules table to determine version information
                logger.debug("Version table exists, but it's empty; have to confirm that version is 2.2.2");
                try (PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
                     ResultSet rs = pstmt.executeQuery();){
                    if (!rs.next()) {
                        throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, nics table doesn't exist");
                    } else {
                        try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT is_static_nat from firewall_rules"); ) {
                            pstmt_static_nat.executeQuery();
                            throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, " +
                                    "nics table doesn't exist, is_static_nat field exists in firewall_rules table");
                        } catch (final SQLException e) {
                            logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
                            return "2.2.2";
                        }
                    }
                } catch (final SQLException e) {
                    throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, query for nics table yields SQL exception", e);
                }
            } else {
                return upgradedVersions.get(0);
            }

        } catch (final SQLException e) {
            throw new CloudRuntimeException("Unable to get the current version", e);
        }

    }
  1. 重新构建 启动服务连接本地Mysql数据库失败的时候,会触发getDefaultHikaricpDataSource连接修改后的GaussDB数据库

结论

因cloudstack是基于mysql开发的系统,如果要适配GaussDB需要重构整个系统,若非特殊需要,不建议强行适配。本文权当提供适配GaussDB或者其他数据库的一个切入点,供有需要的人参考。

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。