Monday, April 20, 2020

Azure Cosmos DB Emulator error : No connection could be made because the target machine actively refused it

We have some integration tests as well as end to end scenario tests which rely on Azure Cosmos DB Emulator for verifying Data layer contracts and the data layer storage manager APIs.

This helps us ensure that our all our storage layer APIs will work as expected in real Azure Cosmos environment. Yes I hear you, and I also leverage MOQ framework for our unit testing and mock testing, but this gives an added level of safety and peace of mind, that I am not mocking the storage, and the storage layer/managers will work as expected as is working in Cosmos DB emulator

Sometimes you may hit the following error :

Message:  

System.ArgumentException :Error while trying to get the containerSystem.ArgumentException: Error while trying to get the databaseSystem.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it.

     ---> System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it.

       at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)

       --- End of inner exception stack trace ---

       at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)

       at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)

       at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)

       at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)

       at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)

       at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

       at Microsoft.Azure.Cosmos.DocumentClient.HttpRequestMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

       at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

       at Microsoft.Azure.Cosmos.GatewayAccountReader.GetDatabaseAccountAsync(Uri serviceEndpoint)

       at Microsoft.Azure.Cosmos.Routing.GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(Uri defaultEndpoint, IList`1 locations, Func`2 getDatabaseAccountFn)

       at Microsoft.Azure.Cosmos.GatewayAccountReader.InitializeReaderAsync()

       at Microsoft.Azure.Cosmos.CosmosAccountServiceConfiguration.InitializeAsync()

       at Microsoft.Azure.Cosmos.DocumentClient.InitializeGatewayConfigurationReaderAsync()

       at Microsoft.Azure.Cosmos.DocumentClient.GetInitializationTaskAsync(IStoreClientFactory storeClientFactory)

       at Microsoft.Azure.Cosmos.DocumentClient.EnsureValidClientAsync()

       at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.EnsureValidClientAsync(RequestMessage request)

       at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.SendAsync(RequestMessage request, CancellationToken cancellationToken)

       at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.SendAsync(Uri resourceUri, ResourceType resourceType, OperationType operationType, RequestOptions requestOptions, ContainerInternal cosmosContainerCore, Nullable`1 partitionKey, Stream streamPayload, Action`1 requestEnricher, CosmosDiagnosticsContext diagnosticsContext, CancellationToken cancellationToken)

       at Microsoft.Azure.Cosmos.CosmosClient.<>c__DisplayClass30_0.<<CreateDatabaseIfNotExistsAsync>b__0>d.MoveNext()

    --- End of stack trace from previous location where exception was thrown ---



This means that your local Azure cosmos DB emulator is not running. You can verify it by browsing to https://localhost:8081/_explorer/index.html. If you dont see any thing it means the service / daemon is stopped. Simply locate Cosmso DB emulator on your machine and start it

Saturday, November 25, 2017

[Solved] Android Studio : Android emulator is incompatible with Hyper-V

Have been lately doing Android development. Ran into a minor problem. So I have two machines, a desktop and a laptop. I set the project on my desktop and everything was working fine (read AVD). Checked in to the code to a GIT repo. However when I was checking out the code on my laptop Lenovo T470s, I ran into a problem with starting the AVD (Android Virtual Device) Manager.

It would show me this error.




Intel HAXM is required to run this AVD.
Android Emulator is incompatible with Hyper-V.
Unfortunately, you cannot have Hyper-V running and use the emulator.
Here is what you can do:
  1) Start a command prompt as Administrator
  2) Run the following command: C:\Windows\system32> bcdedit /set hypervisorlaunchtype off
  3) Reboot your machine


The first thing I did, disabled Hyper-V from my BIOS setting after restarting the system. However then I still kept on getting this error. Then I disabled Hyper-V feature from the 'Windows Feature on or off' settings.



Then I finally installed the HAXM tool from intel's Website (Hardware Accelerated Execution Manager) and things started working smoothly for me.
Hope this helps. 

Sunday, November 05, 2017

[Windows] Refreshing Environment variable for windows command prompt or Powershell

Sometimes, after installing a package like chocolatey,or npm packages, it asks you to restart the cmd.exe or powershell window to reload the new registered path, for the new commands/settings to work. 

Well most of the case, it is not a big issue, but sometimes I just hate having to do away with my command history. So whats an easy workaround to do, run refreshenv

In cmd.exe (Command Shell) run : 

C:\Windows\system32> refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait... Finished..
In Powershell:


PS C:\Users\parag> refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
PS C:\Users\parag>
Enjoy working in the same window without having to part with your command history. 

Saturday, August 12, 2017

Using Tcpdump to dump and read network traffic

Another Quick FYI tip.

There are many network analyzer/reader utilities available on both Linux and Windows platform. There is of-course Wireshark, the most preferred GUI network protocol analyzer, but I prefer tcpdump as it is very easy to use.

Lets get started with some examples then. Say if you want to capture all multicast data (from all interfaces), to a file, you run:

$> tcpdump -n "multicast" -vvv -w VideoStreamData.pcap
where:

  • -w flag writes the captured traffic to the filepath specified. 
  • -n flag avoid DNS look ups to convert host addresses to names. 


Or if you want to capture any packets with a specific destination IP say 172.68.1.1 and destination port 80 or 8080.

$> tcpdump -n "dst host 172.68.1.1 and (dst port 80 or dst port 8080)"
After you have captured network data in a pcap file with the tcpdump command, you can read the data packets in ASCII character sets with the command:

$> tcpdump  -A -r httpServerLogonMessages.pcap 
It comes in handy for reading http/web page traffic.

Or if you want to read the messages both HEX and ASCII along with header data.

$> tcpdump  -x -r MessengerCommunication_11July.pcap 
This comes in handy when you want to convert between different host and network byte order (or the other way round).

Read more about Big Endian to Little Endian conversion and vice-versa here

Sunday, August 06, 2017

Adding routes on a windows machine

Just a small FYI article.

We have multiple P2P lease lines in our office, connecting our different offices within the city, apart from multiple internet connections.
While trying to access these system its prefered to have them accessible over the leased line network.

All of our networks merge on the single LAN. So we need to add routes on our system to tell them, to direct which traffic through which router (as in the specific router connected to the leased line of a office).

For eg the LAN segment 192.66.222.0/8 (to our office in OfficeA1) is accessible via router 192.168.22.10 (which is the meeting point of one of the P2P from here to OfficeA1), run the following command from an elevated command prompt.


C:\>route add -p 192.66.222.0 mask 255.255.255.0 192.168.22.10
Similarly the LAN segment 192.122.72.0 is accessible via router ip 192.168.22.20

C:\>route add -p 192.122.72.0 mask 255.255.255.0 192.168.56.20

Sunday, June 11, 2017

Troubleshooting Packet Drops in SolarFlare Onload 10G PCI Card


If you see lots of packet drops in your onload accelerated application even after going the troubleshooting discussion we did over here, you still see drops and are you are nowhere, you can investigate your application design/OS context scheduling and how the application is reading consuming the data packets. The potential reasons for packet drops could be that,
a) another task interrupting the threads reading the traffic or
b) You have run out of packet buffers because the socket receive buffers aren’t being emptied because read()/recv() isn’t called often enough.

It would be a good idea to monitor how many context switches the application/threads are experience because if this suddenly increases when you have a problem it would indicate another thread is competing for processing time on that core.

You can check this using the following command:

# cat /proc/<pid>/status | grep ctxt_switches
voluntary_ctxt_switches:        58
nonvoluntary_ctxt_switches:     1
Replace “<pid>” with the process ID for your application and the “voluntary” count is the number of times the application blocked and another thread was allowed to run. The “nonvoluntary” count is the number of times the thread was stopped from running by the kernel so something else could run in preference.

You can check the individual threads for the process by monitoring the ‘status’ files in the underlying ‘task’ directory:

# grep ctxt_switches /proc/<pid>/task/*/status
/proc/<pid>/task/<task-n1>/status:voluntary_ctxt_switches:    630
/proc/<pid>/task/<task-n1>/status:nonvoluntary_ctxt_switches: 32
/proc/<pid>/task/<task-n2>/status:voluntary_ctxt_switches:    2
/proc/<pid>/task/<task-n2>/status:nonvoluntary_ctxt_switches: 0
/proc/<pid>/task/<task-n3>/status:voluntary_ctxt_switches:    1
/proc/<pid>/task/<task-n3>/status:nonvoluntary_ctxt_switches: 0

[C#] Mutliple Concurrent Producers and Consumers pattern for a Task Queue

If you are in need of a basic concurrent Producers consumers pattern to be used in your application, here is a sample C# program to refer to...