{"record":{"id":"e04d4941bae93f9f","repo":"alibaba/Sentinel","slug":"zookeeper-has-not-been-initialized-or-error-occurr","errorCode":null,"errorMessage":"Zookeeper has not been initialized or error occurred","messagePattern":"Zookeeper has not been initialized or error occurred","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-datasource-zookeeper/src/main/java/com/alibaba/csp/sentinel/datasource/zookeeper/ZookeeperDataSource.java","lineNumber":159,"sourceCode":"                    } else {\n                        this.zkClient = zkClientMap.get(zkKey);\n                    }\n                }\n            }\n\n            this.nodeCache = CuratorCache.build(this.zkClient, this.path);\n            this.nodeCache.listenable().addListener(this.listener, this.pool);\n            this.nodeCache.start();\n        } catch (Exception e) {\n            RecordLog.warn(\"[ZookeeperDataSource] Error occurred when initializing Zookeeper data source\", e);\n            e.printStackTrace();\n        }\n    }\n\n    @Override\n    public String readSource() throws Exception {\n        if (this.zkClient == null) {\n            throw new IllegalStateException(\"Zookeeper has not been initialized or error occurred\");\n        }\n        String configInfo = null;\n        ChildData childData = nodeCache.get(path).orElse(null);\n        if (null != childData && childData.getData() != null) {\n\n            configInfo = new String(childData.getData());\n        }\n        return configInfo;\n    }\n\n    @Override\n    public void close() throws Exception {\n        if (this.nodeCache != null) {\n            this.nodeCache.listenable().removeListener(listener);\n            this.nodeCache.close();\n        }\n        if (this.zkClient != null) {\n            this.zkClient.close();","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-datasource-zookeeper/src/main/java/com/alibaba/csp/sentinel/datasource/zookeeper/ZookeeperDataSource.java#L141-L177","documentation":"Thrown by ZookeeperDataSource.readSource() when the internal CuratorFramework zkClient is null. Like the Nacos/Redis data sources, init errors during listener setup are caught and only logged (RecordLog.warn + printStackTrace), so a failed ZooKeeper connection leaves zkClient null and every later read throws this IllegalStateException.","triggerScenarios":"ZooKeeper unreachable or the address wrong when initZookeeperListener() runs (Curator start fails), auth failure with digest auth, or Curator/JDK version incompatibility — the cause is swallowed, then loadInitialConfig()/readSource() throws this error.","commonSituations":"ZooKeeper quorum down at app startup; wrong serverAddr (port 2181 vs 26381); ACL-protected znodes with bad AuthInfo; network partitions in Kubernetes. The app runs but the refresh thread repeatedly logs this exception and rules never load.","solutions":["Look for '[ZookeeperDataSource] Error occurred when initializing Zookeeper data source' in the logs — the printed cause shows the real connection/auth problem.","Verify ZooKeeper reachability: echo ruok | nc zk-host 2181 and zkCli.sh -server zk-host:2181 ls <path>.","Fix serverAddr / AuthInfo credentials and restart the application; the client is not retried after a failed init.","Optionally build and start the CuratorFramework yourself first (zkClient.blockUntilConnectedOrTimedOut()) so failures abort startup with a clear error."],"exampleFix":"// before\nnew ZookeeperDataSource<>(\"bad-host:2181\", path, parser); // init fails silently, readSource() throws\n\n// after\nCuratorFramework c = CuratorFrameworkFactory.newClient(\"zk.prod:2181\", new RetryNTimes(3, 1000));\nc.start();\nc.blockUntilConnectedOrTimedOut();   // fail fast at boot if ZooKeeper is down\nnew ZookeeperDataSource<>(\"zk.prod:2181\", path, parser);","handlingStrategy":"try-catch","validationCode":"// pre-verify ZooKeeper connectivity before creating the data source\nCuratorFramework probe = CuratorFrameworkFactory.newClient(serverAddr, new RetryNTimes(3, 1000));\nprobe.start();\nif (!probe.blockUntilConnected(5, TimeUnit.SECONDS)) {\n    throw new IllegalStateException(\"cannot connect to ZooKeeper: \" + serverAddr);\n}\nnew ZookeeperDataSource<>(serverAddr, path, parser);","typeGuard":null,"tryCatchPattern":"try {\n    String raw = dataSource.loadConfig();\n} catch (Exception e) {\n    if (e instanceof IllegalStateException && e.getMessage().contains(\"not been initialized\")) {\n        log.error(\"Curator client failed to init; check serverAddr/auth and restart\", e);\n    }\n    throw e;\n}","preventionTips":["Check startup logs for '[ZookeeperDataSource] Error occurred when initializing' to find the swallowed cause.","Verify ZooKeeper with zkCli.sh (including ACL-protected znodes) before deploying.","Block on an initial Curator connection at boot so failures surface at startup."],"tags":["sentinel","datasource","zookeeper","curator","initialization","runtime-error","java"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}