r/devops7 • • 8d ago

One script, one chore: before Saturday's orders-db maintenance, who do you tell? grep finds 3 services that call it; a graph walk joined with the owners table finds 9 across 9 teams (real output)

Short one this week: one script, one chore, real output. (This sub is run by the SynapCores team; the script uses our database for the one step that matters.)

The chore

orders-db gets a major-version upgrade Saturday at 02:00, about 20 minutes of downtime. Before you post the maintenance notice: which services break, and which on-call channels need to hear about it?

The usual move is grepping configs for the connection string:

$ grep -l orders-db */config.yaml
fulfillment-worker/config.yaml
invoice-cron/config.yaml
orders-api/config.yaml

Three. But checkout calls orders-api, and web and mobile call checkout. They break too, and their teams don't know they depend on orders-db. That is the email you get Saturday at 02:05.

Step 1: the call graph, one line per dependency

deps.txt is "caller callee" (from your catalog or tracing), 18 lines:

web checkout-api
checkout-api orders-api
orders-api orders-db
...

The script loads it as a graph in SynapCores with Cypher, same endpoint as SQL:

MERGE (a:Svc {name: "checkout-api"}) MERGE (b:Svc {name: "orders-api"})
MERGE (a)-[:DEPENDS_ON]->(b)

Owners go in a plain SQL table next to it:

CREATE TABLE services (name TEXT PRIMARY KEY, team TEXT, tier INTEGER, channel TEXT);
-- ('checkout-api', 'checkout', 1, '#checkout-oncall'), ...

Step 2: walk the graph

Everything that reaches orders-db in 1 to 6 hops:

MATCH p = (s:Svc)-[:DEPENDS_ON*1..6]->(t:Svc {name: 'orders-db'})
RETURN s.name AS service, length(p) AS hops, [n IN nodes(p) | n.name] AS via
ORDER BY hops, service

1 hop   fulfillment-worker  fulfillment-worker > orders-db
1 hop   invoice-cron        invoice-cron > orders-db
1 hop   orders-api          orders-api > orders-db
2 hops  admin-portal        admin-portal > orders-api > orders-db
2 hops  checkout-api        checkout-api > orders-api > orders-db
2 hops  notifications       notifications > orders-api > orders-db
2 hops  partner-webhooks    partner-webhooks > orders-api > orders-db
3 hops  mobile-bff          mobile-bff > checkout-api > orders-api > orders-db
3 hops  web                 web > checkout-api > orders-api > orders-db

Step 3, the payoff: join the walk with the owners table

The script saves those rows to a blast table; one SQL query turns them into "who to tell":

SELECT s.team, s.channel, MIN(s.tier) AS tier, COUNT(DISTINCT b.service) AS n,
       GROUP_CONCAT(DISTINCT b.service) AS services
FROM blast b JOIN services s ON s.name = b.service
WHERE b.target = 'orders-db'
GROUP BY s.team, s.channel
ORDER BY tier, n DESC, s.team;

Output of python3 blast.py radius orders-db:

orders-db: 3 direct callers (fulfillment-worker, invoice-cron, orders-api), 9 services affected

  tier 1  checkout      #checkout-oncall       checkout-api
  tier 1  mobile        #mobile-oncall         mobile-bff
  tier 1  orders        #orders-oncall         orders-api
  tier 1  storefront    #storefront-oncall     web
  tier 2  integrations  #integrations-oncall   partner-webhooks
  tier 2  messaging     #messaging             notifications
  tier 2  warehouse     #warehouse-oncall      fulfillment-worker
  tier 3  backoffice    #backoffice            admin-portal
  tier 3  finance-eng   #finance-eng           invoice-cron

grep found 3 services; the real blast radius is 9 services and 9 channels, and 4 of them are tier 1. Two of those tier-1 teams (storefront, mobile) are 3 hops away and would never have found orders-db in their own configs.

Why one database? Variable-length paths with the path attached are a painful recursive CTE in plain SQL. A separate graph store walks them easily, but then owners and tiers live elsewhere and you sync two systems. Here the walk and the join hit the same database and endpoint.

Wire blast.py radius <node> into your change-request template and every maintenance ticket lists its blast radius. blast.py (stdlib only) and both sample files are in the first comment.

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-one-script-6

Where does your dependency map live today: a service catalog, tracing, or "ask in Slack and hope"?

1 Upvotes

2 comments sorted by

1

u/Alternative_Pin9598 8d ago

blast.py (standard library only; set SC_URL and SC_TOKEN). Run: python3 blast.py load deps.txt services.txt && python3 blast.py radius orders-db. The two sample files are in the reply below.

"""blast.py: "before I touch X, what breaks and who do I tell?"
Service calls live in a graph, owners live in a SQL table, same SynapCores database.
usage: blast.py load deps.txt services.txt | blast.py radius orders-db
"""
import json, os, sys, urllib.request

BASE = os.environ["SC_URL"]      # e.g. http://localhost:<port> of your SynapCores
TOKEN = os.environ["SC_TOKEN"]   # from POST /v1/auth/login


def run(q):
    req = urllib.request.Request(BASE + "/v1/query/execute", method="POST",
                                 data=json.dumps({"sql": q, "database": "main"}).encode(),
                                 headers={"Authorization": "Bearer " + TOKEN, "Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=120) as r:
        return json.load(r)["data"].get("rows", [])


def load(deps, services):
    # the call graph: one DEPENDS_ON edge per "caller callee" line (Cypher)
    edges = [l.split() for l in open(deps) if l.strip()]
    for a, b in edges:
        run(f'MERGE (a:Svc {{name: "{a}"}}) MERGE (b:Svc {{name: "{b}"}}) MERGE (a)-[:DEPENDS_ON]->(b)')
    # the ownership table: name, team, tier, channel (SQL)
    run("CREATE TABLE services (name TEXT PRIMARY KEY, team TEXT, tier INTEGER, channel TEXT)")
    rows = [l.split() for l in open(services) if l.strip()]
    run("INSERT INTO services VALUES " + ",".join(f"('{n}', '{t}', {tier}, '{c}')" for n, t, tier, c in rows))
    run("CREATE TABLE blast (target TEXT, service TEXT, hops INTEGER, via TEXT)")
    print(f"loaded {len(edges)} dependencies, {len(rows)} services")


def radius(target):
    # 1. graph: everything that reaches the target through 1..6 DEPENDS_ON hops
    paths = run(f"""MATCH p = (s:Svc)-[:DEPENDS_ON*1..6]->(t:Svc {{name: '{target}'}})
                    RETURN s.name AS service, length(p) AS hops, [n IN nodes(p) | n.name] AS via
                    ORDER BY hops, service""")
    run(f"DELETE FROM blast WHERE target = '{target}'")
    run("INSERT INTO blast VALUES " + ",".join(f"('{target}', '{s}', {h}, '{' > '.join(v)}')" for s, h, v in paths))
    for s, h, v in paths:
        print(f"  {h} hop{'s' if h > 1 else ' '}  {s:<19} {' > '.join(v)}")
    direct = [s for s, h, v in paths if h == 1]
    print(f"\n{target}: {len(direct)} direct callers ({', '.join(direct)}), {len({p[0] for p in paths})} services affected\n")
    # 2. SQL: join the walk with the owners table, one line per team to notify
    for team, channel, tier, n, svcs in run(f"""
        SELECT s.team, s.channel, MIN(s.tier) AS tier, COUNT(DISTINCT b.service) AS n,
               GROUP_CONCAT(DISTINCT b.service) AS services
        FROM blast b JOIN services s ON s.name = b.service
        WHERE b.target = '{target}'
        GROUP BY s.team, s.channel
        ORDER BY tier, n DESC, s.team"""):
        print(f"  tier {tier}  {team:<13} {channel:<22} {svcs}")


if __name__ == "__main__":
    if sys.argv[1] == "load":
        load(sys.argv[2], sys.argv[3])
    else:
        radius(sys.argv[2])

New to graph queries? SQLv2 Lesson 5 explains *1..3 hops, nodes(p) and joining a graph walk with a SQL table, from zero: https://www.reddit.com/r/SQLv2/comments/1xq8g8l/

1

u/Alternative_Pin9598 8d ago

deps.txt (caller callee):

web checkout-api
web search-api
web auth
mobile-bff checkout-api
mobile-bff catalog-api
mobile-bff auth
checkout-api orders-api
checkout-api payments
orders-api orders-db
payments payments-db
admin-portal orders-api
partner-webhooks orders-api
notifications orders-api
fulfillment-worker orders-db
invoice-cron orders-db
search-api search-index
catalog-api catalog-db
auth users-db

services.txt (name team tier channel):

web storefront 1 #storefront-oncall
mobile-bff mobile 1 #mobile-oncall
checkout-api checkout 1 #checkout-oncall
payments payments 1 #payments-oncall
orders-api orders 1 #orders-oncall
admin-portal backoffice 3 #backoffice
partner-webhooks integrations 2 #integrations-oncall
notifications messaging 2 #messaging
fulfillment-worker warehouse 2 #warehouse-oncall
invoice-cron finance-eng 3 #finance-eng
search-api search 2 #search-oncall
catalog-api catalog 2 #catalog-oncall
auth identity 1 #identity-oncall
orders-db dba 1 #dba-oncall
payments-db dba 1 #dba-oncall
search-index search 2 #search-oncall
catalog-db dba 2 #dba-oncall
users-db dba 1 #dba-oncall

Free Community Edition: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=devops7-one-script-6