Fabric-ca: server init函数——initDB

init函数中的数据库初始化操作逻辑

err = ca.initDB(ca.server.dbMetrics)

1 定义数据库文件

if dbCfg.Datasource == "" {
		dbCfg.Datasource = "fabric-ca-server.db"
}

2021/03/26 16:01:03 [DEBUG] Initializing 'sqlite3' database at 'K:\fabric\fabric-ca-master\lib\rootDir\fabric-ca-server.db'

 

2 连接数据库

err = caDB.Connect()

2021/03/26 16:01:31 [DEBUG] Using sqlite database, connect to database in home (K:\fabric\fabric-ca-master\lib\rootDir\fabric-ca-server.db) directory
2021/03/26 16:01:49 [DEBUG] Creating SQLite database (K:\fabric\fabric-ca-master\lib\rootDir\fabric-ca-server.db) if it does not exist...
 

3 创建表

sqlxdb, err := caDB.Create()

具体创建下面的表

func createAllSQLiteTables(tx Create, args ...interface{}) error {
   err := createIdentityTable(tx)
   if err != nil {
      return err
   }
   err = createAffiliationTable(tx)
   if err != nil {
      return err
   }
   err = createCertificateTable(tx)
   if err != nil {
      return err
   }
   err = createCredentialsTable(tx)
   if err != nil {
      return err
   }
   err = createRevocationComponentTable(tx)
   if err != nil {
      return err
   }
   err = createNoncesTable(tx)
   if err != nil {
      return err
   }
   err = createPropertiesTable(tx)
   if err != nil {
      return err
   }
   return nil
}

以第一个方法举例:

func createIdentityTable(tx Create) error {
   log.Debug("Creating users table if it does not exist")
   if _, err := tx.Exec("CreateUsersTable", "CREATE TABLE IF NOT EXISTS users (id VARCHAR(255), token bytea, type VARCHAR(256), affiliation VARCHAR(1024), attributes TEXT, state INTEGER, max_enrollments INTEGER, level INTEGER DEFAULT 0, incorrect_password_attempts INTEGER DEFAULT 0, PRIMARY KEY (id))"); err != nil {
      return errors.Wrap(err, "Error creating users table")
   }
   return nil
}

4 合并表

注意在合并表之前,表内数据都是空的,没有插入任何数据

func getMigrator(driverName string, tx cadb.FabricCATx, curLevels, srvLevels *dbutil.Levels) (cadb.Migrator, error) 
err = db.Migrate(migrator, curLevels, ca.server.levels)

判断level中的每一个项目

if currentLevels.Identity < srvLevels.Identity {
   log.Debug("Migrating users table...")
   err := migrator.MigrateUsersTable()
   if err != nil {
      log.Errorf("Error encountered while migrating users table, rolling back changes: %s", err)
      return migrator.Rollback()
   }
}

2021/03/18 09:43:14 [DEBUG] Migrating users table...

case 0:
   log.Debug("Upgrade identity table to level 1")
   _, err := tx.Exec(funcName, "ALTER TABLE users RENAME TO users_old")
   if err != nil {
      return err
   }
   err = createIdentityTable(tx)
   if err != nil {
      return err
   }
   // If coming from a table that did not yet have the level column then we can only copy columns that exist in both the tables
   _, err = tx.Exec(funcName, "INSERT INTO users (id, token, type, affiliation, attributes, state, max_enrollments) SELECT id, token, type, affiliation, attributes, state, max_enrollments FROM users_old")
   if err != nil {
      return err
   }
   _, err = tx.Exec(funcName, "DROP TABLE users_old")
   if err != nil {
      return err
   }
   fallthrough

最后通过commit统一执行事务

return migrator.Commit()

此时还是没有插入user中用户信息,现在需要插入Identity数据。

如何插入一个用户信息,就是前面生成的admin的cert等信息

if !ca.Config.LDAP.Enabled {
   err = ca.loadUsersTable()
   if err != nil {
      log.Error(err)
      dbError = true
      if caerrors.IsFatalError(err) {
         return err
      }
   }

   err = ca.loadAffiliationsTable()
   if err != nil {
      log.Error(err)
      dbError = true
   }
}
func (ca *CA) loadUsersTable() error {
   log.Debug("Loading identity table")
   registry := &ca.Config.Registry
   for _, id := range registry.Identities {
      log.Debugf("Loading identity '%s'", id.Name)
      err := ca.addIdentity(&id, false)
      if err != nil {
         return errors.WithMessage(err, "Failed to load identity table")
      }
   }
   log.Debug("Successfully loaded identity table")
   return nil
}

registry信息:

 

构造数据库user实例,插入

rec := cadbuser.Info{
		Name:           id.Name,
		Pass:           id.Pass,
		Type:           id.Type,
		Affiliation:    id.Affiliation,
		Attributes:     attrs,
		MaxEnrollments: id.MaxEnrollments,
		Level:          ca.levels.Identity,
	}
err = ca.registry.InsertUser(&rec)

 

继续进行隶属关系表的插入

err = ca.loadAffiliationsTable()

如下

完成数据库的初始化。注意 ca-cert.pem 并不会插入到数据库中,应该一直驻留在内存中。 

user的信息中也没有关于证书的内容。

 

 

检查错误原因 creating directory /data/primary/gpseg0 ... ok creating subdirectories ... ok selecting default max_connections ... 750 selecting default shared_buffers ... 125MB selecting default timezone ... Asia/Shanghai selecting dynamic shared memory implementation ... posix creating configuration files ... ok creating template1 database in /data/primary/gpseg0/base/1 ... child process was terminated by signal 9: Killed initdb: removing data directory "/data/primary/gpseg0" 2023-06-08 08:53:53.568563 GMT,,,p22007,th-604637056,,,,0,,,seg-10000,,,,,"LOG","00000","skipping missing configuration file ""/data/primary/gpseg0/postgresql.auto.conf""",,,,,,,,"ParseConfigFile","guc-file.l",563, 20230608:16:54:12:021728 gpcreateseg.sh:VM-0-5-centos:gpadmin-[INFO]:-Start Function BACKOUT_COMMAND 20230608:16:54:12:021728 gpcreateseg.sh:VM-0-5-centos:gpadmin-[INFO]:-End Function BACKOUT_COMMAND 20230608:16:54:12:021728 gpcreateseg.sh:VM-0-5-centos:gpadmin-[INFO]:-Start Function BACKOUT_COMMAND 20230608:16:54:12:021728 gpcreateseg.sh:VM-0-5-centos:gpadmin-[INFO]:-End Function BACKOUT_COMMAND 20230608:16:54:12:021728 gpcreateseg.sh:VM-0-5-centos:gpadmin-[FATAL][0]:-Failed to start segment instance database VM-0-5-centos /data/primary/gpseg0 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-End Function PARALLEL_WAIT 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-End Function PARALLEL_COUNT 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-Start Function PARALLEL_SUMMARY_STATUS_REPORT 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:------------------------------------------------ 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-Parallel process exit status 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:------------------------------------------------ 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-Total processes marked as completed = 0 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-Total processes marked as killed = 0 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[WARN]:-Total processes marked as failed = 1 <<<<< 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:------------------------------------------------ 20230608:16:54:12:019435 gpinitsystem:VM-0-5-centos:gpadmin-[INFO]:-End Function PARALLEL_SUMMARY_STATUS_REPORT FAILED:VM-0-5-centos~6000~/data/primary/gpseg0~2~0
最新发布
06-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值