From 5eae1874669b19eb9baaffb30d3d59b7ea6fc805 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 16 Jul 2015 17:06:57 +0200 Subject: [PATCH 1/3] replacing TProcessor with TProcessorFactory so the lifecycle of processors are managed elsewhere --- lib/csharp/src/Protocol/TProcessorFactory.cs | 32 ++++++++++++++++++++ lib/csharp/src/Server/TServer.cs | 22 +++++++------- lib/csharp/src/Server/TSimpleServer.cs | 28 +++++++++++------ lib/csharp/src/Server/TThreadPoolServer.cs | 28 +++++++++++------ lib/csharp/src/Server/TThreadedServer.cs | 28 +++++++++++------ lib/csharp/src/Thrift.csproj | 1 + 6 files changed, 98 insertions(+), 41 deletions(-) create mode 100644 lib/csharp/src/Protocol/TProcessorFactory.cs diff --git a/lib/csharp/src/Protocol/TProcessorFactory.cs b/lib/csharp/src/Protocol/TProcessorFactory.cs new file mode 100644 index 00000000000..88197289ad3 --- /dev/null +++ b/lib/csharp/src/Protocol/TProcessorFactory.cs @@ -0,0 +1,32 @@ +/** + * 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. + * + * Contains some contributions under the Thrift Software License. + * Please see doc/old-thrift-license.txt in the Thrift distribution for + * details. + */ + + +namespace Thrift.Protocol +{ + public interface TProcessorFactory + { + TProcessor Create(); + void Release(TProcessor processor); + } +} diff --git a/lib/csharp/src/Server/TServer.cs b/lib/csharp/src/Server/TServer.cs index a2631a96bda..eeb4540c7e7 100644 --- a/lib/csharp/src/Server/TServer.cs +++ b/lib/csharp/src/Server/TServer.cs @@ -31,7 +31,7 @@ namespace Thrift.Server public abstract class TServer { //Attributes - protected TProcessor processor; + protected TProcessorFactory processorFactory; protected TServerTransport serverTransport; protected TTransportFactory inputTransportFactory; protected TTransportFactory outputTransportFactory; @@ -63,23 +63,23 @@ protected static void DefaultLogDelegate(string s) } //Construction - public TServer(TProcessor processor, + public TServer(TProcessorFactory processorFactory, TServerTransport serverTransport) - : this(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) + : this(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) { } - public TServer(TProcessor processor, + public TServer(TProcessorFactory processorFactory, TServerTransport serverTransport, LogDelegate logDelegate) - : this(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) + : this(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) { } - public TServer(TProcessor processor, + public TServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory transportFactory) - : this(processor, + : this(processorFactory, serverTransport, transportFactory, transportFactory, @@ -89,11 +89,11 @@ public TServer(TProcessor processor, { } - public TServer(TProcessor processor, + public TServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory transportFactory, TProtocolFactory protocolFactory) - : this(processor, + : this(processorFactory, serverTransport, transportFactory, transportFactory, @@ -103,7 +103,7 @@ public TServer(TProcessor processor, { } - public TServer(TProcessor processor, + public TServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory inputTransportFactory, TTransportFactory outputTransportFactory, @@ -111,7 +111,7 @@ public TServer(TProcessor processor, TProtocolFactory outputProtocolFactory, LogDelegate logDelegate) { - this.processor = processor; + this.processorFactory = processorFactory; this.serverTransport = serverTransport; this.inputTransportFactory = inputTransportFactory; this.outputTransportFactory = outputTransportFactory; diff --git a/lib/csharp/src/Server/TSimpleServer.cs b/lib/csharp/src/Server/TSimpleServer.cs index 267b4704b94..cd4f04a2f28 100644 --- a/lib/csharp/src/Server/TSimpleServer.cs +++ b/lib/csharp/src/Server/TSimpleServer.cs @@ -34,23 +34,23 @@ public class TSimpleServer : TServer { private bool stop = false; - public TSimpleServer(TProcessor processor, + public TSimpleServer(TProcessorFactory processorFactory, TServerTransport serverTransport) - : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) + : base(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DefaultLogDelegate) { } - public TSimpleServer(TProcessor processor, + public TSimpleServer(TProcessorFactory processorFactory, TServerTransport serverTransport, LogDelegate logDel) - : base(processor, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel) + : base(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), logDel) { } - public TSimpleServer(TProcessor processor, + public TSimpleServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory transportFactory) - : base(processor, + : base(processorFactory, serverTransport, transportFactory, transportFactory, @@ -60,11 +60,11 @@ public TSimpleServer(TProcessor processor, { } - public TSimpleServer(TProcessor processor, + public TSimpleServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory transportFactory, TProtocolFactory protocolFactory) - : base(processor, + : base(processorFactory, serverTransport, transportFactory, transportFactory, @@ -128,8 +128,16 @@ public override void Serve() if (serverEventHandler != null) serverEventHandler.processContext(connectionContext, inputTransport); //Process client request (blocks until transport is readable) - if (!processor.Process(inputProtocol, outputProtocol)) - break; + var processor = processorFactory.Create(); + try + { + if (!processor.Process(inputProtocol, outputProtocol)) + break; + } + finally + { + processorFactory.Release(processor); + } } } } diff --git a/lib/csharp/src/Server/TThreadPoolServer.cs b/lib/csharp/src/Server/TThreadPoolServer.cs index 4c201e9f30a..764049b5776 100644 --- a/lib/csharp/src/Server/TThreadPoolServer.cs +++ b/lib/csharp/src/Server/TThreadPoolServer.cs @@ -37,16 +37,16 @@ public class TThreadPoolServer : TServer private const int DEFAULT_MAX_THREADS = 100; private volatile bool stop = false; - public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport) - : this(processor, serverTransport, + public TThreadPoolServer(TProcessorFactory processorFactory, TServerTransport serverTransport) + : this(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) { } - public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate) - : this(processor, serverTransport, + public TThreadPoolServer(TProcessorFactory processorFactory, TServerTransport serverTransport, LogDelegate logDelegate) + : this(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, logDelegate) @@ -54,25 +54,25 @@ public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, } - public TThreadPoolServer(TProcessor processor, + public TThreadPoolServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory transportFactory, TProtocolFactory protocolFactory) - : this(processor, serverTransport, + : this(processorFactory, serverTransport, transportFactory, transportFactory, protocolFactory, protocolFactory, DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) { } - public TThreadPoolServer(TProcessor processor, + public TThreadPoolServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory inputTransportFactory, TTransportFactory outputTransportFactory, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory, int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel) - : base(processor, serverTransport, inputTransportFactory, outputTransportFactory, + : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, inputProtocolFactory, outputProtocolFactory, logDel) { lock (typeof(TThreadPoolServer)) @@ -178,8 +178,16 @@ private void Execute(Object threadContext) if (serverEventHandler != null) serverEventHandler.processContext(connectionContext, inputTransport); //Process client request (blocks until transport is readable) - if (!processor.Process(inputProtocol, outputProtocol)) - break; + var processor = processorFactory.Create(); + try + { + if (!processor.Process(inputProtocol, outputProtocol)) + break; + } + finally + { + processorFactory.Release(processor); + } } } catch (TTransportException) diff --git a/lib/csharp/src/Server/TThreadedServer.cs b/lib/csharp/src/Server/TThreadedServer.cs index f8ed8e281a5..bf7fff891f2 100644 --- a/lib/csharp/src/Server/TThreadedServer.cs +++ b/lib/csharp/src/Server/TThreadedServer.cs @@ -44,16 +44,16 @@ public int ClientThreadsCount { get { return clientThreads.Count; } } - public TThreadedServer(TProcessor processor, TServerTransport serverTransport) - : this(processor, serverTransport, + public TThreadedServer(TProcessorFactory processorFactory, TServerTransport serverTransport) + : this(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DEFAULT_MAX_THREADS, DefaultLogDelegate) { } - public TThreadedServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate) - : this(processor, serverTransport, + public TThreadedServer(TProcessorFactory processorFactory, TServerTransport serverTransport, LogDelegate logDelegate) + : this(processorFactory, serverTransport, new TTransportFactory(), new TTransportFactory(), new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), DEFAULT_MAX_THREADS, logDelegate) @@ -61,25 +61,25 @@ public TThreadedServer(TProcessor processor, TServerTransport serverTransport, L } - public TThreadedServer(TProcessor processor, + public TThreadedServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory transportFactory, TProtocolFactory protocolFactory) - : this(processor, serverTransport, + : this(processorFactory, serverTransport, transportFactory, transportFactory, protocolFactory, protocolFactory, DEFAULT_MAX_THREADS, DefaultLogDelegate) { } - public TThreadedServer(TProcessor processor, + public TThreadedServer(TProcessorFactory processorFactory, TServerTransport serverTransport, TTransportFactory inputTransportFactory, TTransportFactory outputTransportFactory, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory, int maxThreads, LogDelegate logDel) - : base(processor, serverTransport, inputTransportFactory, outputTransportFactory, + : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, inputProtocolFactory, outputProtocolFactory, logDel) { this.maxThreads = maxThreads; @@ -214,8 +214,16 @@ private void ClientWorker(Object context) if (serverEventHandler != null) serverEventHandler.processContext(connectionContext, inputTransport); //Process client request (blocks until transport is readable) - if (!processor.Process(inputProtocol, outputProtocol)) - break; + var processor = processorFactory.Create(); + try + { + if (!processor.Process(inputProtocol, outputProtocol)) + break; + } + finally + { + processorFactory.Release(processor); + } } } } diff --git a/lib/csharp/src/Thrift.csproj b/lib/csharp/src/Thrift.csproj index 195005a1457..85a5ceeaf2e 100644 --- a/lib/csharp/src/Thrift.csproj +++ b/lib/csharp/src/Thrift.csproj @@ -93,6 +93,7 @@ + From 723f6890fb09c3314cdaf9f1628daa69ed6c0183 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 Jul 2015 10:19:18 +0200 Subject: [PATCH 2/3] adding TProcessorSingletonFactory to help with the breaking change and reflect change in test code --- lib/csharp/src/Protocol/TProcessorFactory.cs | 11 ++++---- .../Protocol/TProcessorSingletonFactory.cs | 28 +++++++++++++++++++ lib/csharp/src/Thrift.csproj | 1 + lib/csharp/test/ThriftTest/TestServer.cs | 5 ++-- 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 lib/csharp/src/Protocol/TProcessorSingletonFactory.cs diff --git a/lib/csharp/src/Protocol/TProcessorFactory.cs b/lib/csharp/src/Protocol/TProcessorFactory.cs index 88197289ad3..ed3349f8b79 100644 --- a/lib/csharp/src/Protocol/TProcessorFactory.cs +++ b/lib/csharp/src/Protocol/TProcessorFactory.cs @@ -21,12 +21,11 @@ * details. */ - namespace Thrift.Protocol { - public interface TProcessorFactory - { - TProcessor Create(); - void Release(TProcessor processor); - } + public interface TProcessorFactory + { + TProcessor Create(); + void Release(TProcessor processor); + } } diff --git a/lib/csharp/src/Protocol/TProcessorSingletonFactory.cs b/lib/csharp/src/Protocol/TProcessorSingletonFactory.cs new file mode 100644 index 00000000000..d884274b1e7 --- /dev/null +++ b/lib/csharp/src/Protocol/TProcessorSingletonFactory.cs @@ -0,0 +1,28 @@ +using System; + +namespace Thrift.Protocol +{ + public class TProcessorSingletonFactory : TProcessorFactory + { + private readonly TProcessor _processor; + + public TProcessorSingletonFactory(TProcessor processor) + { + if (processor == null) + { + throw new ArgumentNullException("processor"); + } + + _processor = processor; + } + + public TProcessor Create() + { + return _processor; + } + + public void Release(TProcessor processor) + { + } + } +} diff --git a/lib/csharp/src/Thrift.csproj b/lib/csharp/src/Thrift.csproj index 85a5ceeaf2e..1a842eba7f3 100644 --- a/lib/csharp/src/Thrift.csproj +++ b/lib/csharp/src/Thrift.csproj @@ -94,6 +94,7 @@ + diff --git a/lib/csharp/test/ThriftTest/TestServer.cs b/lib/csharp/test/ThriftTest/TestServer.cs index 0e9fe05d8e0..6ad9ab75613 100644 --- a/lib/csharp/test/ThriftTest/TestServer.cs +++ b/lib/csharp/test/ThriftTest/TestServer.cs @@ -405,6 +405,7 @@ public static bool Execute(string[] args) // Processor TestHandler testHandler = new TestHandler(); ThriftTest.Processor testProcessor = new ThriftTest.Processor(testHandler); + TProcessorSingletonFactory testProcessorSingletonfactory = new TProcessorSingletonFactory(testProcessor); // Transport TServerTransport trans; @@ -435,9 +436,9 @@ public static bool Execute(string[] args) // Simple Server TServer serverEngine; if ( useFramed ) - serverEngine = new TSimpleServer(testProcessor, trans, new TFramedTransport.Factory(), proto); + serverEngine = new TSimpleServer(testProcessorSingletonfactory, trans, new TFramedTransport.Factory(), proto); else - serverEngine = new TSimpleServer(testProcessor, trans, new TTransportFactory(), proto); + serverEngine = new TSimpleServer(testProcessorSingletonfactory, trans, new TTransportFactory(), proto); // ThreadPool Server // serverEngine = new TThreadPoolServer(testProcessor, tServerSocket); From a690784b217dcd60ba3bfae53b8b0b2ae0fc3349 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 17 Jul 2015 11:02:10 +0200 Subject: [PATCH 3/3] adding source files to build script --- lib/csharp/Makefile.am | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/csharp/Makefile.am b/lib/csharp/Makefile.am index 5ce42761906..c94700704a4 100644 --- a/lib/csharp/Makefile.am +++ b/lib/csharp/Makefile.am @@ -38,6 +38,8 @@ THRIFTCODE= \ src/Protocol/TProtocolDecorator.cs \ src/Protocol/TMultiplexedProtocol.cs \ src/Protocol/TMultiplexedProcessor.cs \ + src/Protocol/TProcessorFactory.cs \ + src/Protocol/TProcessorSingletonFactory.cs \ src/Protocol/TType.cs \ src/Protocol/TField.cs \ src/Protocol/TMessage.cs \