Does Stackexchange.Redis (C#) support connecting to a cluster of sentinels for high availability with the latest version ? I'm finding it a bit odd how such an important feature of redis is not properly documented or practically no examples at all. Would appreciate any help.
You can see how to connect to sentinels by examining the tests of StackExchange.Redis. Specifically this one https://github.com/StackExchange/StackExchange.Redis/blob/master/tests/StackExchange.Redis.Tests/Sentinel.cs
Essentially the code you need to look at is here:
public Sentinel(ITestOutputHelper output) : base(output)
{
ConnectionLog = new StringWriter();
Skip.IfNoConfig(nameof(TestConfig.Config.SentinelServer), TestConfig.Current.SentinelServer);
Skip.IfNoConfig(nameof(TestConfig.Config.SentinelSeviceName), TestConfig.Current.SentinelSeviceName);
var options = new ConfigurationOptions()
{
CommandMap = CommandMap.Sentinel,
EndPoints = { { TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort } },
AllowAdmin = true,
TieBreaker = "",
ServiceName = TestConfig.Current.SentinelSeviceName,
SyncTimeout = 5000
};
Conn = ConnectionMultiplexer.Connect(options, ConnectionLog);
Thread.Sleep(3000);
Assert.True(Conn.IsConnected);
Server = Conn.GetServer(TestConfig.Current.SentinelServer, TestConfig.Current.SentinelPort);
}
And then here:
[Fact]
public void SentinelGetMasterAddressByNameTest()
{
var endpoint = Server.SentinelGetMasterAddressByName(ServiceName);
Assert.NotNull(endpoint);
var ipEndPoint = endpoint as IPEndPoint;
Assert.NotNull(ipEndPoint);
Log("{0}:{1}", ipEndPoint.Address, ipEndPoint.Port);
}
Related
I am (trying) to use this code to create ServiceBus Queue:
using Azure.Messaging.ServiceBus;
using Azure.Messaging.ServiceBus.Administration;
...
class blabla
{
private string connectionString = "Endpoint=sb://XXXX.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=XXXYYY";
private string queueName = "testqueue";
...
public doit()
{
var adminClient = new ServiceBusAdministrationClient(connectionString);
bool queueExists = adminClient.QueueExistsAsync(queueName).Result;
if (!queueExists)
{
var options = new CreateQueueOptions(queueName)
{
DefaultMessageTimeToLive = TimeSpan.FromDays(2),
LockDuration = TimeSpan.FromSeconds(45),
MaxDeliveryCount = 8,
MaxSizeInMegabytes = 2048
};
options.AuthorizationRules.Add(new SharedAccessAuthorizationRule(
"allClaims",
new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen }));
QueueProperties createdQueue = adminClient.CreateQueueAsync(options).Result;
}
}
}
but constantly getting this error:
System.AggregateException: One or more errors occurred. (SubCode=40900. Conflict. You're requesting an operation that isn't allowed in the resource's current state. To know more visit https://aka.ms/sbResourceMgrExceptions. . TrackingId:bc79fd98-73c8-4301-b6b9-05d0eae6ed6a_G17, SystemTracker:xxx.servicebus.windows.net:yyy, Timestamp:2021-05-09T00:24:57
Status: 409 (Conflict)
ErrorCode: 40900
Using old (NET) way with NamespaceManager from Microsoft.ServiceBus works with no problems.
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists(queueName))
{
namespaceManager.CreateQueue(queueName);
}
So, does anyone knows what am I doing wrong here?
*
Below is the updated working code, you need to make sure you have shared access policy with full access.
using Azure.Messaging.ServiceBus.Administration;
using System;
using System.Threading.Tasks;
namespace ServiceBusDemo
{
class Program
{
private static string connectionString = "Endpoint=sb://ns-servicebusshu.servicebus.windows.net/;SharedAccessKeyName=fullAccess;SharedAccessKey=oB+IsK8Aqp0/xfXnF9HCz6x9pqPIOysTXaJofSmHEYs=";
private static string queueName = "testqueue";
async static Task Main(string[] args)
{
await doit();
}
public static async Task doit()
{
var adminClient = new ServiceBusAdministrationClient(connectionString);
bool queueExists = await adminClient.QueueExistsAsync(queueName);
if (!queueExists)
{
var options = new CreateQueueOptions(queueName)
{
DefaultMessageTimeToLive = TimeSpan.FromDays(2),
LockDuration = TimeSpan.FromSeconds(45),
MaxDeliveryCount = 8,
MaxSizeInMegabytes = 2048
};
options.AuthorizationRules.Add(new SharedAccessAuthorizationRule("allClaims", new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen }));
QueueProperties createdQueue = await adminClient.CreateQueueAsync(options);
}
}
}
}
Once you ran the application its successfully created the queue as below :
Maybe it's not your case... But if you have a TOPIC with the same name that you try to create your new QUEUE, QueueExistsAsync will return false, but you'll be spitted with this bizarre error at creation time. The fix is easy... changing the queue name or deleting the offending topic.
Sorry for the confusion.
My code (and Rahul Shukla as well) is working now (????).
I had to create a few new shared access policies with full access (????).
The third created started working (??).
The previous 2 I created are still not working (????).
There are no differences between the 3 policies created. Hence the question marks in my answer.
Posted question on MS NET SB forum about 1 out of 3 policies working. No answer/acknowledgment so far.
Setup:
I'm using MongoDB v4.2.7 along with the .Net MongoDB driver v2.11.0(beta v) on a windows 10 machine.
Code
var mongoClientSettings = new MongoClientSettings();
mongoClientSettings.Compressors = new List<CompressorConfiguration>() {
new CompressorConfiguration(CompressorType.ZStandard)
};
var client = new MongoClient(mongoClientSettings);
IMongoDatabase testdb = client.GetDatabase("testdb");
var eaiRequestLogsCollection = testdb.GetCollection<EAIRequestsLogMDB>("EAIRequestsLogs");
eaiRequestLogsCollection.InsertMany(eAIRequestsLogMDBs);
Config
I've edited my mongod.cfg file as follows:
storage:
dbPath: C:\Program Files\MongoDB\Server\4.2\data
journal:
enabled: true
engine: "wiredTiger"
wiredTiger:
collectionConfig:
blockCompressor: "zstd"
Problem:
After the collection and the documents are added successfully I ran the db.printCollectionStats() on the mongo shell and I got block_compressor=snappy in the WiredTiger section while it should be block_compressor=zstd.
Below is a screenshot of the db.Stats(1024*1024*1024) output as well
"dataSize" : 0.08773485571146011 and "storageSize" : 0.009387969970703125
I posted a question on MongoDB Developer community Forums and was advised that my c# code was setting Network Compression rather than block_compressor.
This is the correct c# code that worked for me:
var client = new MongoClient("mongodb://127.0.0.1:27017");
IMongoDatabase testdb = client.GetDatabase("testdb");
testdb.CreateCollection("EAIRequestsLogs", new CreateCollectionOptions()
{
StorageEngine = new BsonDocument
{
{ "wiredTiger", new BsonDocument
{
{ "configString" , "block_compressor=zstd"}
}
}
}
});
var eaiRequestLogsCollection = testdb.GetCollection<EAIRequestsLogMDB>("EAIRequestsLogs");
Kind regards.
I have HomeController as below,
#region Properties
const string topic = "AnotherTestTopic";
const string host = "http://localhost:9092";
#endregion
[HttpPost]
public ActionResult Save(FormCollection form)
{
var kafkaOptions = new KafkaOptions(new Uri(host));
var brokerRouter = new BrokerRouter(kafkaOptions);
var producer = new Producer(brokerRouter);
producer.SendMessageAsync(topic, new[] { new Message("Test message") }).Wait();
return RedirectToAction("Index", "Home");
}
I am using kafka-net dll and my SendMessageAsync method as below
public async Task<List<ProduceResponse>> SendMessageAsync(string topic, IEnumerable<Message> messages, Int16 acks = 1,
TimeSpan? timeout = null, MessageCodec codec = MessageCodec.CodecNone)
{
if (_stopToken.IsCancellationRequested)
throw new ObjectDisposedException("Cannot send new documents as producer is disposing.");
if (timeout == null) timeout = TimeSpan.FromMilliseconds(DefaultAckTimeoutMS);
var batch = messages.Select(message => new TopicMessage
{
Acks = acks,
Codec = codec,
Timeout = timeout.Value,
Topic = topic,
Message = message
}).ToList();
_asyncCollection.AddRange(batch);
await Task.WhenAll(batch.Select(x => x.Tcs.Task));
return batch.Select(topicMessage => topicMessage.Tcs.Task.Result)
.Distinct()
.ToList();
}
Question:
I just started learning kafka.I really do not know how can i create topic from c# code.How can i add topic from c# ?
Any help will be appreciated.
Thanks.
You can set auto.create.topics.enable=true in your broker configuration to let Kafka create the topic for you when it is not previously created.
You may also want to set num.partitions and default.replication.factor to appropriate values in your broker configuration as well.
I've been trying figure out for a few days now why I am getting exceptions such as http://i.imgur.com/cfCBWRS.png
public virtual bool CreateOrUpdateValueById<T>(TQuery query, TResult value)
{
using (var redisClient = Connection.RedisManager.GetClient())
{
var redis = redisClient.As<TResult>();
var key = query.GetKeyWithId();
redis.SetEntry(key, value);
return true;
}
}
which runs in loop of several hundred of items.
foreach (var playlistItem in playlistItems)
{
var query = new PlaylistItemsQuery(playlistItem.Id, playlistItem.PlaylistId);
_playlistItemsQueryHandler.CreateOrUpdateValueById<PlaylistItemDto>(query, playlistItem);
}
also happens for any get query
public virtual IEnumerable<TResult> GetAllValues(TQuery query)
{
using (var redisClient = Connection.RedisManager.GetReadOnlyClient()
{
var keys = redisClient.ScanAllKeys(query.GetKeyWithAllIds()).ToList();
return redisClient.GetValues<TResult>(keys);
}
}
i use singleton class for redispool
public static IRedisClientsManager RedisManager { get; } = new PooledRedisClientManager
{
ConnectTimeout = 60000
};
I am hosting redis on localhost windows which is not officially supported, can this really be the case?
The error message suggests the Redis Client is unable to make a TCP Connection with the Remote Redis Server. If you're not using a licensed version of ServiceStack.Redis v4 then it could mean you've exceeded the ServiceStack.Redis Free Quota Limits.
Otherwise confirm that you can connect to it from redis-cli.exe, if you can't you can try restarting the redis-server.
I am in the process of integrating with the FedEx international Ship Service. But I am really stuck on one part. I am trying to create a certificate of origin using their test environment. I have followed the xml schema and have come up with the code below
private static void SetCustomInvoice(ProcessShipmentRequest request)
{
request.RequestedShipment.ShippingDocumentSpecification = new ShippingDocumentSpecification();
request.RequestedShipment.ShippingDocumentSpecification.ShippingDocumentTypes = new RequestedShippingDocumentType[1] { new RequestedShippingDocumentType() };
request.RequestedShipment.ShippingDocumentSpecification.ShippingDocumentTypes[0] = RequestedShippingDocumentType.CERTIFICATE_OF_ORIGIN;
request.RequestedShipment.ShippingDocumentSpecification.CertificateOfOrigin = new CertificateOfOriginDetail();
request.RequestedShipment.ShippingDocumentSpecification.CertificateOfOrigin.DocumentFormat = new ShippingDocumentFormat { StockType = ShippingDocumentStockType.STOCK_4X6, ImageType = ShippingDocumentImageType.PDF, ImageTypeSpecified = true, StockTypeSpecified = true };
request.RequestedShipment.SpecialServicesRequested = new ShipmentSpecialServicesRequested();
request.RequestedShipment.SpecialServicesRequested.SpecialServiceTypes = new ShipmentSpecialServiceType[1] { new ShipmentSpecialServiceType() };
request.RequestedShipment.SpecialServicesRequested.SpecialServiceTypes[0] = ShipmentSpecialServiceType.ELECTRONIC_TRADE_DOCUMENTS;
request.RequestedShipment.SpecialServicesRequested.EtdDetail = new EtdDetail();
request.RequestedShipment.SpecialServicesRequested.EtdDetail.RequestedDocumentCopies = new RequestedShippingDocumentType[1] { RequestedShippingDocumentType.CERTIFICATE_OF_ORIGIN };
request.RequestedShipment.SpecialServicesRequested.EtdDetail.DocumentReferences = new UploadDocumentReferenceDetail[1] { new UploadDocumentReferenceDetail() };
request.RequestedShipment.SpecialServicesRequested.EtdDetail.RequestedDocumentCopies[0] = RequestedShippingDocumentType.CERTIFICATE_OF_ORIGIN;
}
But I keep getting an error message back from the web service stating “Invalid Stock Type”. Even though the shipmentDocumentStockType is an enum and I am using one of the values from it. I am still getting this error. Any ideas where I might be going wrong?
Any information will be a great help. I have tried getting in touch with FedEx technical support and they were not really a great help.
This might be a little late for you but just wanted to provide an answer in case someone else might be looking for it.
I was having a similar problem but instead of the Certificate of Origin it was with the Commercial Invoice. Turns out these forms need to be printed on a full 8.5 x 11 page in PDF format, so changing the StockType from STOCK_4x6 to PAPER_LETTER fixed it for me:
From:
request.RequestedShipment.ShippingDocumentSpecification.CertificateOfOrigin.DocumentFormat = new ShippingDocumentFormat { StockType = ShippingDocumentStockType.STOCK_4X6, ImageType = ShippingDocumentImageType.PDF, ImageTypeSpecified = true, StockTypeSpecified = true };
To:
request.RequestedShipment.ShippingDocumentSpecification.CertificateOfOrigin.DocumentFormat = new ShippingDocumentFormat { StockType = ShippingDocumentStockType.PAPER_LETTER, ImageType = ShippingDocumentImageType.PDF, ImageTypeSpecified = true, StockTypeSpecified = true };
Hope this helps