ClickHouse最佳实战之分布表写入流程分析

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/af/af9f6637b50b09be60b00a42f3812d5e.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"云妹导读:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前不久,京东智联云正式上线了基于Clickhouse的分析型云数据库JCHDB,一经推出便受到广大用户的极大关注。有兴趣的小伙伴可以回顾上一篇文章"},{"type":"link","attrs":{"href":"https://mp.weixin.qq.com/s?__biz=MzU1OTgxMTg2Nw==&mid=2247494299&idx=1&sn=3bb704ece56a19c4c4a335fdfbcd33b3&scene=21#wechat_redirect","title":null},"content":[{"type":"text","text":"《比MySQL快839倍!揭开分析型数据库JCHDB的神秘面纱》"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ClickHouse像ElasticSearch一样具有数据分片(shard)的概念,这也是分布式存储的特点之一,即通过并行读写提高效率。ClickHouse依靠Distributed引擎实现了Distributed(分布式)表机制,在所有分片(本地表)上建立视图进行分布式查询,使用很方便。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d2/d284f81bf66f5e5995fda6d216af0294.webp","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Distributed表引擎是"},{"type":"text","marks":[{"type":"italic"},{"type":"strong"}],"text":"一种特殊的表引擎,自身不会存储任何数据,而是通过读取或写入其他远端节点上的表进行数据处理的表引擎。"},{"type":"text","text":"该表引擎需要依赖各个节点的本地表来创建,本地表的存在是Distributed表创建的依赖条件,创建语句如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"CREATE TABLE {teble} ON CLUSTER {cluster}\nAS {local_table}\nENGINE= Distributed({cluster}, {database}, {local_table},{policy})\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里的policy一般可以使用随机(例如rand())或哈希(例如halfMD5hash(id))。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再来看下ClickHouse集群节点配置文件,相关参数如下:"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n \n \n 1\n true\n \n 1\n example01-01-1\n 9000\n \n \n example01-01-2\n 9000\n \n \n \n 2\n true\n \n example01-02-1\n 9000\n \n \n example01-02-2\n 9000\n \n \n \n"}]},{"type":"heading","attrs":{"align":null,"level":3}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fb/fb2cb9e4ef39402d3f825eef487d3213.webp","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":3}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有了上面的基础了解,就将进入主题了,本文主要是对Distributed表如何写入及如何分发做一下分析,略过SQL的词法解析、语法解析等步骤,从写入流开始,其构造方法如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"DistributedBlockOutputStream(const Context & context_, StorageDistributed &\nstorage_, const ASTPtr & query_ast_, const ClusterPtr & cluster_, bool\ninsert_sync_, UInt64 insert_timeout_);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果insert_sync_为true,表示是同步写入,并配合insert_timeout_参数使用(insert_timeout_为零表示没有超时时间);如果insert_sync_为false,表示写入是异步。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"1,同步写入还是异步写入"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同步写入是指数据直写入实际的表中,而异步写入是指数据首先被写入本地文件系统,然后发送到远端节点。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"BlockOutputStreamPtr StorageDistributed::write(const ASTPtr &, const Context &\ncontext)\n{\n ......\n\n /// Force sync insertion if it is remote() table function\n bool insert_sync = settings.insert_distributed_sync || owned_cluster;\n auto timeout = settings.insert_distributed_timeout;\n /// DistributedBlockOutputStream will not own cluster, but will own \nConnectionPools of the cluster\n return std::make_shared(\n context, *this, createInsertToRemoteTableQuery(remote_database,\nremote_table, getSampleBlockNonMaterialized()), cluster,\n nsert_sync, timeout);\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是否执行同步写入是由insert_sync决定的,最终是由是否配置insert_distributed_sync(默认为false)和owned_cluster值的或关系决定的,一般在使用MergeTree之类的普通表引擎时,通常是异步写入,但在使用表函数时(使用owned_cluster来判断是否是表函数),通常会使用同步写入。这也是在设计业务逻辑时需要注意的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"},{"type":"strong"}],"text":"owned_cluster是什么时候赋值的呢?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"StoragePtr TableFunctionRemoteexecuteImpl(const ASTPtr & astfunction, const Context & \ncontext, const stdstring & tablename) const\n{ \n ......\n StoragePtr res = remotetablefunction_ptr\n ? StorageDistributed::createWithOwnCluster(\n table_name,\n structureremotetable,\n remotetablefunction_ptr,\n cluster,\n context)\n : StorageDistributed::createWithOwnCluster(\n table_name,\n structureremotetable,\n remote_database,\n remote_table,\n cluster,\n context);\n ......\n} \nStoragePtr StorageDistributed::createWithOwnCluster(\n const std::string & tablename, \n const ColumnsDescription & columns_,\n ASTPtr & remotetablefunctionptr, \n ClusterPtr & ownedcluster, \n const Context & context_)\n{ \n auto res = create(String{}, tablename, columns, ConstraintsDescription{}, \nremotetablefunctionptr, String{}, context, ASTPtr(), String(), false);\n res->ownedcluster = ownedcluster_;\n return res;\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以发现在创建remote表时会根据remote_table_function_ptr参数对最终的owned_cluster_赋值为true。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"2,异步写入是如何实现的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"了解了什么时候使用同步写入什么时候异步写入后,再继续分析正式的写入过程,同步写入一般场景中涉及较少,这里主要对异步写入逻辑进行分析。outStream的write方法主逻辑如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"DistributedBlockOutputStream::write()\n ↓\n if insert_sync\n | |\n true false\n ↓ ↓\n writeSync() writeAsync() \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实这个write方法是重写了virtual void IBlockOutputStream::write(const Block & block),所以节点在接收到流并调用流的write方法就会进入该逻辑中。并且根据insert_sync来决定走同步写还是异步写。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"3,写入本地节点还是远端节点"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主要还是对异步写入进行分析,其实writeAsync()最终的实现方法是writeAsyncImpl(),大致逻辑图如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" writeAsyncImpl()\n ↓\n if shard_info.hasInternalReplication()\n | |\n true false\n ↓ ↓\nwriteToLocal() writeToLocal()\n ↓ ↓\nwriteToShard() for(every shard){writeToShard()}\n ↓ ↓ \n end end\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中getShardsInfo()方法就是获取config.xml配置文件中获取集群节点信息,hasInternalReplication()就对应着配置文件中的internal_replication参数,如果为true,就会进入最外层的if逻辑,否则就会进入else逻辑。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中writeToLocal()方法是相同的,是指如果shard包含本地节点,优先选择本地节点进行写入;后半部分writeToShard()就是根据internal_replication参数的取值来决定是写入其中一个远端节点,还是所有远端节点都写一次。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"4,数据如何写入本地节点"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当然一般情况Distributed表还是基于ReplicatedMergeTree系列表进行创建,而不是基于表函数的,所以大多数场景还是会先写入本地再分发到远端节点。那写入Distributed表的数据是如何保证原子性落盘而不会在数据正在写入的过程中就把不完整的数据发送给远端其他节点呢?看下writeToShard()方法大致逻辑,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" writeToShard()\n ↓\nfor(every dir_names){\n |\n └──if first iteration\n | |\n false true\n ↓ ↓ \n | ├──storage.requireDirectoryMonitor()\n | ├──CompressedWriteBuffer\n | ├──writeStringBinary()\n | ├──stream.writePrefix()\n | ├──stream.write(block)\n | ├──stream.writeSuffix()\n ↘ ↙ \n link(tmp_file, file) \n └──} \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"继续具体再看下源码的具体实现,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"void DistributedBlockOutputStream::writeToShard(const Block & block, const\nstd::vector<:string> & dir_names) \n{\n /** tmp directory is used to ensure atomicity of transactions\n * and keep monitor thread out from reading incomplete data\n */\n std::string first_file_tmp_path{};\n\n auto first = true;\n\n /// write first file, hardlink the others\n for (const auto & dir_name : dir_names)\n {\n const auto & path = storage.getPath() + dir_name + '/';\n\n /// ensure shard subdirectory creation and notify storage\n if (Poco::File(path).createDirectory())\n storage.requireDirectoryMonitor(dir_name);\n\n const auto & file_name = toString(storage.file_names_increment.get()) +\n\".bin\";\n const auto & block_file_path = path + file_name;\n\n /** on first iteration write block to a temporary directory for \nsubsequent hardlinking to ensure\n * the inode is not freed until we're done */\n if (first)\n {\n first = false;\n\n const auto & tmp_path = path + \"tmp/\";\n Poco::File(tmp_path).createDirectory();\n const auto & block_file_tmp_path = tmp_path + file_name;\n\n first_file_tmp_path = block_file_tmp_path;\n\n WriteBufferFromFile out{block_file_tmp_path};\n CompressedWriteBuffer compress{out};\n NativeBlockOutputStream stream{compress, ClickHouseRevision::get(),\nblock.cloneEmpty()};\n\n writeVarUInt(UInt64(DBMS_DISTRIBUTED_SENDS_MAGIC_NUMBER), out);\n context.getSettingsRef().serialize(out);\n writeStringBinary(query_string, out);\n\n stream.writePrefix();\n stream.write(block);\n stream.writeSuffix();\n }\n\n if (link(first_file_tmp_path.data(), block_file_path.data()))\n throwFromErrnoWithPath(\"Could not link \" + block_file_path + \" to \"\n+ first_file_tmp_path, block_file_path,\n ErrorCodes::CANNOT_LINK);\n }\n ......\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先来了解下Distributed表在目录中的存储方式,默认位置都是/var/lib/clickhouse/data/{database}/{table}/在该目录下会为每个shard生成不同的目录,其中存放需要发送给该shard的数据文件,例如:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"[root@ck test]# tree\n.\n├── 'default@ck2-0:9000,default@ck2-1:9000'\n│ ├── 25.bin\n│ └── tmp\n│ └── 26.bin\n└── 'default@ck3-0:9000,default@ck3-1:9000'\n└── tmp \n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以发现每个shard对应的目录名是{darabse}@{hostname}:{tcpPort}的格式,如果多个副本会用,分隔。并且每个shard目录中还有个tmp目录,这个目录的设计在writeToShard()方法中做了解释,是为了避免数据文件在没写完就被发送到远端。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"数据文件在本地写入的过程中会先写入tmp路径中,写完后通过硬链接link到shard目录,保证只要在shard目录中出现的数据文件都是完整写入的数据文件。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"数据文件的命名是通过全局递增的数字加.bin命名,是为了在后续分发到远端节点保持顺序性。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"5,数据如何分发到各个节点"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"细心的你可能已经发现在writeToShard()方法中有个requireDirectoryMonitor(),这个方法就是将shard目录注册监听,并通过专用类StorageDistributedDirectoryMonitor来实现数据文件的分发,根据不同配置可以实现逐一分发或批量分发。并且包含对坏文件的容错处理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b8/b8c1b4ef181102a1a2759e1fd3ddd674.jpeg","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"分析到这,可能还有人会觉得云里雾里,觉得整个流程串不起来,其实这样写是为了先不影响Distributed表写入的主流程,明白了这个再附加上sharding_key拆分和权重拆分就很好理解了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ff/ff238d54a3bed271638a86a75316ccf0.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面提到过writeAsync()的最终实现方法是writeAsyncImpl,这个说法是没问题的,但是中间还有段关键逻辑,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" writeAsync()\n ↓\nif storage.getShardingKeyExpr() && (cluster->getShardsInfo().size() > 1\n | |\n true false\n ↓ ↓\n writeAsyncImpl(block) writeSplitAsync(block)\n ↓\n splitBlock(block)\n ↓\n writeAsyncImpl(splitted_blocks,shard_idx)\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"getShardingKeyExpr()方法就是去获取sharding_key生成的表达式指针,该表达式是在创建表时就生成的,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"sharding_key_expr = buildShardingKeyExpression(sharding_key_, global_context,\ngetColumns().getAllPhysical(), false);\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那sharding_key和sharding_key_expr是什么关系呢?如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"const ExpressionActionsPtr & getShardingKeyExpr() const { return \nsharding_key_expr; }\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以说sharding_key_expr最终主要就是由sharding_key决定的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般情况下getShardingKeyExpr()方法都为true,如果再满足shard数量大于1,就会对block进行拆分,由splitBlock()方法主要逻辑就是创建selector并使用selector进行切割,大致逻辑如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":" splitBlock()\n ↓\n createSelector(block)\n ↓\nfor(every shard){column->scatter(num_shards, selector);}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于如何创建selector以及selector中都做了什么事儿,来具体看下源码截取,如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"IColumn::Selector DistributedBlockOutputStream::createSelector(const Block &\nsource_block)\n{\n Block current_block_with_sharding_key_expr = source_block;\n storage.getShardingKeyExpr()- \n>execute(current_block_with_sharding_key_expr);\n\n const auto & key_column =\ncurrent_block_with_sharding_key_expr.getByName(storage.getShardingKeyColumnName\n());\n const auto & slot_to_shard = cluster->getSlotToShard();\n ......\n throw Exception{\"Sharding key expression does not evaluate to an integer \ntype\", ErrorCodes::TYPE_MISMATCH};\n}\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/55209b32f7356455e0d12aab5a70cc80.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看splitBlock()方法,ClickHouse是利用createSelector()方法构造selector来进行后续的处理。在createSelector()方法中最重要的就是key_column和slot_to_shard。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"key_column是通过sharding_key间接获得的,是为了根据主键列进行切割;slot_to_shard是shard插槽,这里就是为了处理权重,在后续向插槽中插入数据时就会结合config.xml中的weight进行按比例处理。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"细节比较复杂这里不做太细致的分析,有兴趣可以自行看下(如template IColumn::Selector createBlockSelector())。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到此,对于Distributed表的写入流程的关键点就大致分析完了。篇幅有限有些细节没有做过多说明,有兴趣的可以自行再了解下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2a/2a81e637daad92bfb11590923632fcca.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过对Distributed表写入流程的分析,了解了该类型表的实际工作原理,所以在实际应用中有几个点还需要关注一下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"Distributed表在写入时会在本地节点生成临时数据,会产生写放大,所以会对CPU及内存造成一些额外消耗,建议尽量少使用Distributed表进行写操作;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"Distributed表写的临时block会把原始block根据sharding_key和weight进行再次拆分,会产生更多的block分发到远端节点,也增加了merge的负担;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"Distributed表如果是基于表函数创建的,一般是同步写,需要注意。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"了解原理才能更好的使用,遇到问题才能更好的优化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"点击"},{"type":"text","marks":[{"type":"italic"},{"type":"strong"}],"text":"【"},{"type":"link","attrs":{"href":"https://www.jdcloud.com/cn/products/jcs-for-jchdb?utm_source=PMM_infoq&utm_medium=NAutm_campaign=ReadMoreutm_term=NA","title":""},"content":[{"type":"text","text":"阅读原文"}]},{"type":"text","marks":[{"type":"italic"},{"type":"strong"}],"text":"】"},{"type":"text","text":"即可前往京东智联云控制台开通试用JCHDB。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章