Permalink
Browse files

Merge branch 'master' into stable

  • Loading branch information...
2 parents b6375bf + 7c3f654 commit 9bb7bd731584d1710a77489b627f2cdbc3740920 Yangming committed Oct 1, 2012
Showing with 955 additions and 451 deletions.
  1. BIN csharp/StatsdClient.dll
  2. +7 −8 csharp/StatsdClient/Buckets.cs
  3. +6 −15 csharp/StatsdClient/Client.cs
  4. +1 −2 csharp/StatsdClient/IStatsdClient.cs
  5. +63 −45 csharp/StatsdClient/MailSlot.cs
  6. +56 −44 csharp/TestPerformance/ClientTest.cs
  7. +69 −0 csharp/TestPerformance/MailSlotTest.cs
  8. +1 −0 csharp/TestPerformance/TestPerformance.csproj
  9. +41 −143 java/src/main/java/com/appfirst/statsd/AFClient.java
  10. +0 −39 java/src/main/java/com/appfirst/statsd/BucketBuffer.java
  11. +63 −35 java/src/main/java/com/appfirst/statsd/{AbstractStatsdClient.java → DefaultStatsdClient.java}
  12. +0 −53 java/src/main/java/com/appfirst/statsd/GeyserStrategy.java
  13. +0 −16 java/src/main/java/com/appfirst/statsd/InstantStrategy.java
  14. +36 −0 java/src/main/java/com/appfirst/statsd/StatsdHandler.java
  15. +0 −7 java/src/main/java/com/appfirst/statsd/Strategy.java
  16. +5 −0 java/src/main/java/com/appfirst/statsd/annotation/Counting.java
  17. +5 −0 java/src/main/java/com/appfirst/statsd/annotation/Timing.java
  18. +2 −1 java/src/main/java/com/appfirst/statsd/{ → bucket}/Bucket.java
  19. +4 −9 java/src/main/java/com/appfirst/statsd/{ → bucket}/CounterBucket.java
  20. +3 −3 java/src/main/java/com/appfirst/statsd/{ → bucket}/GaugeBucket.java
  21. +5 −3 java/src/main/java/com/appfirst/statsd/{ → bucket}/TimerBucket.java
  22. +44 −0 java/src/main/java/com/appfirst/statsd/strategy/BucketBuffer.java
  23. +11 −0 java/src/main/java/com/appfirst/statsd/strategy/BucketTypeMismatchException.java
  24. +105 −0 java/src/main/java/com/appfirst/statsd/strategy/GeyserStrategy.java
  25. +35 −0 java/src/main/java/com/appfirst/statsd/strategy/InstantStrategy.java
  26. +11 −0 java/src/main/java/com/appfirst/statsd/strategy/Strategy.java
  27. +19 −0 java/src/main/java/com/appfirst/statsd/strategy/StrategyFactory.java
  28. +118 −0 java/src/main/java/com/appfirst/statsd/transport/AFTransport.java
  29. +11 −0 java/src/main/java/com/appfirst/statsd/transport/Transport.java
  30. +10 −8 java/src/main/java/com/appfirst/statsd/{UDPClient.java → transport/UdpTransport.java}
  31. +149 −0 java/src/test/java/com/appfirst/statsd/test/LoadTest.java
  32. +15 −17 java/src/test/java/com/appfirst/statsd/test/TestBuckets.java
  33. +60 −3 java/src/test/java/com/appfirst/statsd/test/TestStatsdClient.java
View
Binary file not shown.
@@ -1,8 +1,7 @@
using System;
using System.Collections.Generic;
-using System.Linq;
-using System.Text;
using System.Runtime.CompilerServices;
+using System.Text;
namespace Statsd
{
@@ -29,7 +28,7 @@ public static DateTime ConvertToDateTime(ulong timestamp)
}
}
- abstract class AbstractBucket : IBucket
+ public abstract class AbstractBucket : IBucket
{
private String name;
@@ -83,7 +82,7 @@ protected void AddMessage(String message)
public abstract void Infuse(int value, String message);
}
- class CounterBucket : AbstractBucket
+ public class CounterBucket : AbstractBucket
{
private int value = 0;
@@ -98,7 +97,7 @@ public override void Infuse(int value, String message)
}
}
- class TimerBucket : AbstractBucket
+ public class TimerBucket : AbstractBucket
{
private int sumstat = 0;
private int count = 0;
@@ -117,7 +116,7 @@ public override void Infuse(int value, String message)
}
}
- class GaugeBucket : AbstractBucket
+ public class GaugeBucket : AbstractBucket
{
private int sumstat = 0;
private int count = 0;
@@ -140,14 +139,14 @@ public override void Infuse(int value, String message)
}
}
- class BucketBuffer {
+ public class BucketBuffer
+ {
private Dictionary<String, IBucket> cellar = new Dictionary<String, IBucket>();
public bool IsEmpty(){
return this.cellar.Count == 0;
}
-
[MethodImpl(MethodImplOptions.Synchronized)]
public void Accumulate<T>(String bucketname, int value, string message)
where T : IBucket
@@ -1,15 +1,6 @@
-
using System;
-using System.Linq;
-using System.Net.Sockets;
-using System.Text;
-using System.IO;
-using Microsoft.Win32.SafeHandles;
-using System.Runtime.InteropServices;
-using System.Timers;
-using System.Diagnostics;
using System.Collections.Generic;
-using System.Runtime.CompilerServices;
+using System.Timers;
namespace Statsd
@@ -128,14 +119,14 @@ public bool UpdateCount(string message, int magnitude, double sampleRate, params
#endregion
}
- public class GeyserStrategy : IStrategy
+ public class BufferedStrategy : IStrategy
{
private static BucketBuffer buffer = new BucketBuffer();
private static Timer schedule = new Timer();
public double Interval = 20000;
private static SendDelegate doSend;
- static GeyserStrategy()
+ static BufferedStrategy()
{
AppDomain domain = AppDomain.CurrentDomain;
domain.ProcessExit += new EventHandler(Flush);
@@ -144,9 +135,9 @@ static GeyserStrategy()
schedule.Elapsed += new ElapsedEventHandler(Flush);
}
- public GeyserStrategy(){}
+ public BufferedStrategy(){}
- public GeyserStrategy(int interval)
+ public BufferedStrategy(int interval)
{
this.Interval = interval;
}
@@ -170,7 +161,7 @@ private static void Flush(object sender, EventArgs e)
public bool Emit<T>(SendDelegate doSend, string bucketname, int value, string message)
where T : IBucket
{
- GeyserStrategy.doSend = doSend;
+ BufferedStrategy.doSend = doSend;
buffer.Accumulate<T>(bucketname, value, message);
if (!schedule.Enabled)
@@ -1,5 +1,4 @@
-using System;
-namespace Statsd
+namespace Statsd
{
public interface IBucket
{
@@ -1,17 +1,15 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using System.Diagnostics;
using System.IO;
-using System.Threading;
using System.Net.Sockets;
using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading;
using Microsoft.Win32.SafeHandles;
-using System.Diagnostics;
namespace Statsd
{
- static class NativeMailSlot
+ public static class NativeMailSlot
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern SafeFileHandle CreateFile(
@@ -28,7 +26,8 @@ int hTemplateFile
public static extern SafeFileHandle CreateMailslot(string lpName, uint nMaxMessageSize,
uint lReadTimeout, IntPtr lpSecurityAttributes);
- [DllImport("kernel32.dll")]
+ [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetMailslotInfo(SafeFileHandle hMailslot, IntPtr lpMaxMessageSize,
IntPtr lpNextSize, IntPtr lpMessageCount, IntPtr lpReadTimeout);
@@ -50,44 +49,47 @@ public class MailSlotTransport : ITransport, IDisposable
{
private static string SLOTNAME = @"\\.\mailslot\afcollectorapi";
- private static SafeFileHandle slotHandle = null;
+ private static int MAX_MESSAGE_SIZE = 2048;
- private static FileStream fs = null;
+ private static FileStream mailSlot = null;
- private FileStream mailSlot
+ private void ensureMailSlot()
{
- get
+ if (mailSlot == null)
{
- if (fs == null)
- {
- if (slotHandle == null || slotHandle.IsInvalid || slotHandle.IsClosed)
- {
- slotHandle = NativeMailSlot.CreateFile(SLOTNAME,
- (uint)FileAccess.Write,
- (uint)FileShare.ReadWrite,
- 0,
- (uint)FileMode.Open,
- (uint)FileAttributes.Normal,
- 0);
- }
- if (!slotHandle.IsInvalid)
- {
- fs = new FileStream(slotHandle, FileAccess.Write);
- }
- else
- {
- throw new Exception("MailSlot Handle is Invalid");
- }
- }
+ mailSlot = CreateFileStream(SLOTNAME);
+ }
+ }
+
+ private FileStream CreateFileStream(String slotName)
+ {
+ SafeFileHandle slotHandle = NativeMailSlot.CreateFile(
+ slotName,
+ (uint)FileAccess.Write,
+ (uint)FileShare.ReadWrite,
+ 0,
+ (uint)FileMode.Open,
+ (uint)FileAttributes.Normal,
+ 0);
+ if (!slotHandle.IsInvalid)
+ {
+ FileStream fs = new FileStream(slotHandle, FileAccess.Write);
return fs;
}
+ else
+ {
+ throw new Exception("MailSlot Cannot be initialized: Handle is Invalid");
+ }
}
public bool Ready
{
get
{
- return mailSlot != null;
+ lock (mailSlot)
+ {
+ return mailSlot != null;
+ }
}
}
@@ -98,16 +100,25 @@ public bool Send(string mail)
UnicodeEncoding encoding = new UnicodeEncoding();
string data_string = string.Format("{0}:{1}:{2}", Process.GetCurrentProcess().Id, 3, mail);
byte[] data_bytes = encoding.GetBytes(data_string);
- int byteCount = encoding.GetByteCount(data_string);
-
+ int byteCount = data_bytes.Length;
+ if (byteCount > MAX_MESSAGE_SIZE)
+ {
+ Console.WriteLine(String.Format(
+ "message size {0} bytes but is limited to {1} bytes, will be truncated",
+ byteCount, MAX_MESSAGE_SIZE));
+ byteCount = MAX_MESSAGE_SIZE;
+ }
+ ensureMailSlot();
mailSlot.Write(data_bytes, 0, byteCount);
mailSlot.Flush();
- Console.WriteLine("sending " + data_string);
+ Console.WriteLine("sending " + data_string.Substring(0, encoding.GetCharCount(data_bytes, 0, byteCount)));
}
catch (IOException ioe)
{
- Debug.WriteLine(String.Format("{0} Exception caught.", ioe));
+ this.Close();
+ Console.WriteLine(String.Format("{0} Exception caught.", ioe));
+ return false;
}
return true;
@@ -117,14 +128,20 @@ public bool Send(string mail)
public void Close()
{
- if (fs != null)
- {
- fs.Close();
- fs = null;
- }
- else if (slotHandle != null && !slotHandle.IsInvalid)
+ lock (mailSlot)
{
- slotHandle.Close();
+ if (mailSlot != null)
+ {
+ try
+ {
+ mailSlot.Close();
+ }
+ catch (IOException) { }
+ finally
+ {
+ mailSlot = null;
+ }
+ }
}
}
@@ -150,7 +167,8 @@ public new bool Send(string message)
}
else
{
- retval = this.udpClient.Send(message);
+ return false;
+ //retval = this.udpClient.Send(message);
}
return retval;
}
Oops, something went wrong.

0 comments on commit 9bb7bd7

Please sign in to comment.