You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/05/10 05:34:34 UTC

[GitHub] [iotdb] eedalong commented on a change in pull request #3083: [IOTDB-1261] add client csharp

eedalong commented on a change in pull request #3083:
URL: https://github.com/apache/iotdb/pull/3083#discussion_r629059008



##########
File path: client-csharp/SessionPool.cs
##########
@@ -0,0 +1,642 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+using Thrift;
+using Thrift.Transport;
+using Thrift.Protocol;
+using System;
+using System.Linq;
+using System.Collections.Generic;
+using System.Threading;
+using Thrift.Transport.Client;
+using iotdb_client_csharp.client.utils;
+using NLog;
+using System.Net.Sockets;
+using System.Threading.Tasks;
+namespace iotdb_client_csharp.client{
+    public class SessionPool{
+        private string username, password, zoneId, host;
+        public int SUCCESS_CODE{

Review comment:
       Sounds Good

##########
File path: client-csharp/SessionPool.cs
##########
@@ -0,0 +1,642 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+using Thrift;
+using Thrift.Transport;
+using Thrift.Protocol;
+using System;
+using System.Linq;
+using System.Collections.Generic;
+using System.Threading;
+using Thrift.Transport.Client;
+using iotdb_client_csharp.client.utils;
+using NLog;
+using System.Net.Sockets;
+using System.Threading.Tasks;
+namespace iotdb_client_csharp.client{
+    public class SessionPool{
+        private string username, password, zoneId, host;
+        public int SUCCESS_CODE{
+           get{return 200;}
+        }
+        private int port, fetch_size;
+        private int pool_size = 4;
+        private bool debug_mode = false;
+        private bool is_close = true;
+        private ConcurentClientQueue client_lst;
+        private NLog.Logger _logger;
+        public Utils util_functions = new Utils();
+        private static TSProtocolVersion protocol_version = TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3;
+
+    public SessionPool(string host, int port, int pool_size){
+           // init success code 
+           this.host = host;
+           this.port = port;
+           this.username = "root";
+           this.password = "root";
+           this.zoneId = "UTC+08:00";
+           this.fetch_size = 1024;
+           this.pool_size = pool_size;
+       }  
+       public SessionPool(string host, int port, string username, string password, int pool_size=8){
+           this.host = host;
+           this.port = port;
+           this.password = password;
+           this.username = username;
+           this.zoneId = "UTC+08:00";
+           this.fetch_size = 1024;
+           this.debug_mode = false;
+           this.pool_size = pool_size;
+       }
+       public SessionPool(string host, int port, string username, string password, int fetch_size, int pool_size=8){
+           this.host = host;
+           this.port = port;
+           this.username = username;
+           this.password = password;
+           this.fetch_size = fetch_size;
+           this.zoneId = "UTC+08:00";
+           this.debug_mode = false;
+           this.pool_size = pool_size;
+
+       }
+        public SessionPool(string host, int port, string username="root", string password="root", int fetch_size=1000, string zoneId = "UTC+08:00", int pool_size=8){
+            this.host = host;
+            this.port = port;
+            this.username = username;
+            this.password = password;
+            this.zoneId = zoneId;
+            this.fetch_size = fetch_size;
+            this.debug_mode = false;
+            this.pool_size = pool_size;
+        }
+        public void open_debug_mode(NLog.Config.LoggingConfiguration config=null){
+            this.debug_mode = true;
+            if(config == null){
+                config = new NLog.Config.LoggingConfiguration();
+                var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
+                config.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);
+                NLog.LogManager.Configuration = config;
+                _logger = NLog.LogManager.GetCurrentClassLogger();
+            }else{
+                NLog.LogManager.Configuration = config;
+                _logger = NLog.LogManager.GetCurrentClassLogger();
+            }
+        }
+        public void close_debug_mode(){
+            this.debug_mode = false;
+        }
+
+        public async Task open(bool enableRPCCompression){
+            client_lst = new ConcurentClientQueue();
+            for(int index = 0; index < pool_size; index++){
+                client_lst.Add(await create_and_open(enableRPCCompression));
+            }
+        }
+        public bool is_open(){
+            return !is_close;
+        }
+        public async Task close(){
+            if(is_close){
+                return;
+            }
+            foreach(var client in client_lst.client_queue.AsEnumerable()){
+                var req = new TSCloseSessionReq(client.sessionId);
+                try{
+                    await client.client.closeSessionAsync(req);
+                }
+                catch(TException e){
+                    var message = String.Format("Error occurs when closing session at server. Maybe server is down");
+                    throw new TException(message, e);
+                }
+                finally{
+                    is_close = true;
+                    if (client.transport != null){
+                        client.transport.Close();
+                    }
+                }
+            }
+        }
+        public async Task set_time_zone(string zoneId){
+            this.zoneId = zoneId;
+            foreach(var client in client_lst.client_queue.AsEnumerable()){
+                var req = new TSSetTimeZoneReq(client.sessionId, zoneId);
+                try{
+                    var resp = await client.client.setTimeZoneAsync(req);
+                    if(debug_mode){
+                        _logger.Info("setting time zone_id as {0}, server message:{1}", zoneId, resp.Message);
+                    }
+                }
+                catch(TException e ){
+                    var message = String.Format("could not set time zone");
+                    throw new TException(message, e); 
+                }
+            }
+        }
+        public async Task<string> get_time_zone(){
+            TSGetTimeZoneResp resp;
+            if(zoneId != ""){
+                return zoneId;
+            }
+            var client = client_lst.Take();
+            try{
+                resp = await client.client.getTimeZoneAsync(client.sessionId);
+            }
+            catch(TException e){
+                client_lst.Add(client);
+                var message = String.Format("counld not get time zone");
+                throw new TException(message, e); 
+            }
+            client_lst.Add(client);
+            return resp.TimeZone;
+        }
+
+        public async Task<Client> create_and_open(bool enableRPCCompression){          
+            TcpClient tcp_client = new TcpClient(this.host, this.port);
+            TSIService.Client client;
+            long sessionId, statementId;
+            var transport = new TFramedTransport(new TSocketTransport(tcp_client, null));
+            if(!transport.IsOpen){
+                try{
+                    await transport.OpenAsync(new CancellationToken());
+                }
+                catch(TTransportException){
+                    throw;
+                }
+            }
+            if(enableRPCCompression){
+                client = new TSIService.Client(new TCompactProtocol(transport));
+            }else{
+                client = new TSIService.Client(new TBinaryProtocol(transport));
+            }
+            var open_req = new TSOpenSessionReq(protocol_version, zoneId);
+            open_req.Username = username;
+            open_req.Password = password;
+            try{
+                var open_resp = await client.openSessionAsync(open_req);
+                if(open_resp.ServerProtocolVersion != protocol_version){
+                    var message = String.Format("Protocol Differ, Client version is {0} but Server version is {1}", protocol_version, open_resp.ServerProtocolVersion);
+                    throw new TException(message, null);
+                }
+                if (open_resp.ServerProtocolVersion == 0){
+                    throw new TException("Protocol not supported", null);
+                }
+                sessionId = open_resp.SessionId;
+                statementId = await client.requestStatementIdAsync(sessionId);
+            }
+            catch(Exception){
+                transport.Close();
+                throw;
+            }
+            is_close = false; 
+            var return_client = new Client();
+            return_client.client = client;
+            return_client.sessionId = sessionId;
+            return_client.statementId = statementId;
+            return_client.transport = transport;
+            return return_client;       
+        }
+        public async Task<int> set_storage_group_async(string group_name){
+            TSStatus status;
+            var client = client_lst.Take();
+            try{
+                status = await client.client.setStorageGroupAsync(client.sessionId, group_name);
+            }
+            catch(TException e){
+                client_lst.Add(client);
+                var err_msg = String.Format("set storage group {0} failed", group_name);
+                throw new TException(err_msg, e);
+            }
+           
+            if(debug_mode){

Review comment:
       Cool!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org