func getLocalCell(ctx context.Context, t *testing.T, ts topo.Impl) string { cells, err := ts.GetKnownCells(ctx) if err != nil { t.Fatalf("GetKnownCells: %v", err) } if len(cells) < 1 { t.Fatalf("provided topo.Impl doesn't have enough cells (need at least 1): %v", cells) } return cells[0] }
// CopyTablets will create the tablets in the destination topo func CopyTablets(ctx context.Context, fromTS, toTS topo.Impl) { cells, err := fromTS.GetKnownCells(ctx) if err != nil { log.Fatalf("fromTS.GetKnownCells: %v", err) } tts := topo.Server{ Impl: toTS, } wg := sync.WaitGroup{} rec := concurrency.AllErrorRecorder{} for _, cell := range cells { wg.Add(1) go func(cell string) { defer wg.Done() tabletAliases, err := fromTS.GetTabletsByCell(ctx, cell) if err != nil { rec.RecordError(fmt.Errorf("GetTabletsByCell(%v): %v", cell, err)) } else { for _, tabletAlias := range tabletAliases { wg.Add(1) go func(tabletAlias *topodatapb.TabletAlias) { defer wg.Done() // read the source tablet tablet, _, err := fromTS.GetTablet(ctx, tabletAlias) if err != nil { rec.RecordError(fmt.Errorf("GetTablet(%v): %v", tabletAlias, err)) return } // try to create the destination err = toTS.CreateTablet(ctx, tablet) if err == topo.ErrNodeExists { // update the destination tablet log.Warningf("tablet %v already exists, updating it", tabletAlias) _, err = tts.UpdateTabletFields(ctx, tablet.Alias, func(t *topodatapb.Tablet) error { *t = *tablet return nil }) } if err != nil { rec.RecordError(fmt.Errorf("CreateTablet(%v): %v", tabletAlias, err)) return } }(tabletAlias) } } }(cell) } wg.Wait() if rec.HasErrors() { log.Fatalf("copyTablets failed: %v", rec.Error()) } }