func (p *PostgresKeeper) updatePGState(pctx context.Context) { p.pgStateMutex.Lock() pgState := &cluster.PostgresState{} defer func() { p.lastPGState = pgState p.pgStateMutex.Unlock() }() initialized, err := p.pgm.IsInitialized() if err != nil { pgState = nil return } if !initialized { pgState.Initialized = false } else { var err error ctx, cancel := context.WithTimeout(pctx, p.clusterConfig.RequestTimeout) pgState, err = pg.GetPGState(ctx, p.getOurReplConnString()) cancel() if err != nil { log.Errorf("error getting pg state: %v", err) pgState = nil return } pgState.Initialized = true // if timeline <= 1 then no timeline history file exists. pgState.TimelinesHistory = cluster.PostgresTimeLinesHistory{} if pgState.TimelineID > 1 { ctx, cancel = context.WithTimeout(pctx, p.clusterConfig.RequestTimeout) tlsh, err := pg.GetTimelinesHistory(ctx, pgState.TimelineID, p.getOurReplConnString()) cancel() if err != nil { log.Errorf("error getting timeline history: %v", err) pgState = nil return } pgState.TimelinesHistory = tlsh } } }
func (p *PostgresKeeper) pgStateHandler(w http.ResponseWriter, req *http.Request) { pgState := &cluster.PostgresState{} p.cvMutex.Lock() defer p.cvMutex.Unlock() initialized, err := p.pgm.IsInitialized() if err != nil { w.WriteHeader(http.StatusInternalServerError) return } if !initialized { pgState.Initialized = false } else { var err error ctx, cancel := context.WithTimeout(context.Background(), p.clusterConfig.RequestTimeout) pgState, err = pg.GetPGState(ctx, p.getOurReplConnString()) cancel() if err != nil { log.Errorf("error getting pg state: %v", err) w.WriteHeader(http.StatusInternalServerError) return } pgState.Initialized = true // if timeline <= 1 then no timeline history file exists. pgState.TimelinesHistory = cluster.PostgresTimeLinesHistory{} if pgState.TimelineID > 1 { ctx, cancel = context.WithTimeout(context.Background(), p.clusterConfig.RequestTimeout) tlsh, err := pg.GetTimelinesHistory(ctx, pgState.TimelineID, p.getOurReplConnString()) cancel() if err != nil { log.Errorf("error getting timeline history: %v", err) w.WriteHeader(http.StatusInternalServerError) return } pgState.TimelinesHistory = tlsh } } if err := json.NewEncoder(w).Encode(&pgState); err != nil { w.WriteHeader(http.StatusInternalServerError) } }