| 731 | } |
| 732 | |
| 733 | func (c *Controller) RaftRestoreFromBoltDb(sourceDbPath string) error { |
| 734 | log := pfxlog.Logger() |
| 735 | |
| 736 | if c.raftController == nil { |
| 737 | return errors.New("can't initialize non-raft controller using initialize from db") |
| 738 | } |
| 739 | |
| 740 | if _, err := os.Stat(sourceDbPath); err != nil { |
| 741 | if os.IsNotExist(err) { |
| 742 | return errors.Wrapf(err, "source db not found at [%v]", sourceDbPath) |
| 743 | } |
| 744 | return errors.Wrapf(err, "invalid db path [%v]", sourceDbPath) |
| 745 | } |
| 746 | |
| 747 | sourceDb, err := db.Open(sourceDbPath) |
| 748 | if err != nil { |
| 749 | return err |
| 750 | } |
| 751 | defer func() { |
| 752 | if err = sourceDb.Close(); err != nil { |
| 753 | log.WithError(err).Error("error closing migration source bolt db") |
| 754 | } |
| 755 | }() |
| 756 | |
| 757 | timelineId, err := sourceDb.GetTimelineId(boltz.TimelineModeForceReset, shortid.Generate) |
| 758 | if err != nil { |
| 759 | return err |
| 760 | } |
| 761 | log.WithField("timelineId", timelineId).WithField("path", sourceDbPath).Info("restoring from bolt db") |
| 762 | |
| 763 | buf := &bytes.Buffer{} |
| 764 | gzWriter := gzip.NewWriter(buf) |
| 765 | if err = sourceDb.StreamToWriter(gzWriter); err != nil { |
| 766 | if closeErr := gzWriter.Close(); closeErr != nil { |
| 767 | log.WithError(closeErr).Error("error closing db snapshot buffer") |
| 768 | } |
| 769 | return err |
| 770 | } |
| 771 | |
| 772 | if err = gzWriter.Close(); err != nil { |
| 773 | return errors.Wrap(err, "error finishing gz compression of migration snapshot") |
| 774 | } |
| 775 | |
| 776 | cmd := &command.SyncSnapshotCommand{ |
| 777 | TimelineId: timelineId, |
| 778 | Snapshot: buf.Bytes(), |
| 779 | SnapshotSink: c.network.RestoreSnapshot, |
| 780 | } |
| 781 | |
| 782 | if err = c.raftController.Bootstrap(); err != nil { |
| 783 | return fmt.Errorf("unable to bootstrap cluster (%w)", err) |
| 784 | } |
| 785 | |
| 786 | return c.raftController.Dispatch(cmd) |
| 787 | } |
| 788 | |
| 789 | // TODO: this functions is a temporary hack and should be provided by xweb |
| 790 | func getApiPath(binding string) string { |