用 Google DP 库在 Beam 中实现 ε=1 的计数并给出误差

解读

面试官想考察三件事:

  1. 你是否知道 ε-差分隐私(ε-DP) 的数学含义与工程折衷;
  2. 能否在 Beam 管道里 正确嵌入 Google DP 库(com.google.privacy.differentialprivacy) 并保证 端到端可移植性;
  3. 能否把 随机算法输出的置信区间 以 Beam 原语(Metrics、View、SideOutput)暴露给下游,而不是只打印日志。

国内大厂现阶段对“合规出境+可用开源组件”卡得很严,Google DP 库并未上传到 Maven 中央仓,因此现场必须说明 “源码级引入 + 合规审查” 这一落地路径,否则会被判为“脱离国情”。

知识点

  1. ε-差分隐私:ε 越小隐私越好,误差越大;ε=1 属于“中等隐私”档位。
  2. Google DP 计数器底层采用 “截断阈值+拉普拉斯机制”,其 标准差 σ = Δf / ε,对于计数 Δf=1,因此 σ = 1。
  3. Beam 的 CombineFn 是跨运行时的唯一安全入口;Google DP 库有 Count.dpCount() 返回 SerializableFunction,但对象本身 不是 Serializable,必须包一层 DoFn 或 CombineFn 并显式 transient 重建。
  4. 误差(L∞ 置信界) 在 95% 置信度下为 1.96σ = 1.96;需要把该值封装成 Beam Metric 或 侧输出 PCollectionView,供下游 DAG 消费。
  5. 国内合规:Google DP 库含 BoringSSL 原生代码,需走 “源码剥离 → 自编译 so → 内部 Maven 私服” 三步,否则无法通过安全审计。

答案

// 1. 合规引入:已把 Google DP 源码打成 internal-dp-2.0.jar 并上传到私服
// 2. 核心 CombineFn,保证跨 Spark/Flink 可序列化
public class DpCountFn extends CombineFn<Long, DpCountFn.Accum, PrivacyCountResult> {
  public static final long SERIAL_VERSION_UID = 1L;

  public static class Accum implements Serializable {
    private static final long serialVersionUID = 1L;
    transient Count dpCount;   // transient 避免序列化问题
    long roughReal;            // 仅用于调试,不输出原始数据
  }

  @Override
  public Accum createAccumulator() {
    Accum acc = new Accum();
    acc.dpCount = Count.dpCount().epsilon(1.0).build();
    acc.roughReal = 0;
    return acc;
  }

  @Override
  public Accum addInput(Accum acc, Long input) {
    acc.dpCount.incrementBy(input);
    acc.roughReal += input;
    return acc;
  }

  @Override
  public Accum mergeAccumulators(Iterable<Accum> it) {
    Accum merged = createAccumulator();
    for (Accum a : it) {
      merged.dpCount.mergeWith(a.dpCount);
      merged.roughReal += a.roughReal;
    }
    return merged;
  }

  @Override
  public PrivacyCountResult extractOutput(Accum acc) {
    long noisy = acc.dpCount.computeResult();
    // 95% 置信界
    double error95 = 1.96 * 1.0;   // σ=1
    return new PrivacyCountResult(noisy, error95);
  }
}

// 3. 管道主逻辑
PCollection<Long> elems = p.apply("ReadLines", TextIO.read().from("gs://input"))
                             .apply("MapToOne", MapElements.into(TypeDescriptors.longs())
                                                           .via(x -> 1L));

PCollection<PrivacyCountResult> dpResult =
    elems.apply("DpCount", Combine.globally(new DpCountFn()));

// 4. 把误差同时写成 Metric,方便监控
dpResult.apply("LogError", ParDo.of(new DoFn<PrivacyCountResult, Void>() {
  @ProcessElement
  public void process(@Element PrivacyCountResult r, PipelineOptions po) {
    Metrics.counter("privacy", "error_95").inc((long) r.error95);
    LOG.info("DP count={}, 95% error={}", r.count, r.error95);
  }
}));

运行后在任意 Runner 上均可得到:
计数值 = 真值 + Lap(0,1) 噪声,
95% 置信误差 = 1.96(与数据集大小无关,仅由 ε=1 决定)。

拓展思考

  1. 复合算子:如果需求改成 “分组计数 + 每组 ε=1”,总隐私预算会线性膨胀。国内监管要求 “总 ε≤3”,此时必须引入 “预聚合 + 预算均摊” 或 “层级组合器”(如 PrivacyAccountant)。
  2. 流式场景:在 Flink Runner 上,CombineFn 的 Accum 需支持快照;Google DP 的 Count 对象 不支持增量序列化,需要改写成 “窗口结束时一次性 computeResult”,否则 checkpoint 会失败。
  3. 误差可视化:把误差随 ε 变化的曲线 预计算成 PCollectionView,通过 SideInput 灌给前端 BI,实现“拖动隐私滑杆→实时刷新误差” 的合规报表,这是国内银行 POC 时最常见的加分项。