[MongoDB] Read preference in a transaction must be primary

MongoDB 4.0.3 ReplicaSet

MongoDB.Driver for .net Core 2.7.3

在Connection String中配置了readPreference=secondaryPreferred:

mongodb://user:password@mongod-1:27017,mongod-2:27017,mongod-3:27017/DBName?connect=replicaSet&readPreference=secondaryPreferred

然后在一个Transaction中读取数据报错:”Read preference in a transaction must be primary”, Transaction中的读取不能使用secondaryPreferred

Dynamically create generic C# object using reflection | 动态创建C#泛型实例

Classes referenced by generic type:

public class Cat
{
    ...
}
public class Dog
{
    ...
}

Generic class:

public class Animals<T>
{
    public Animals(int id, T body)
    {
        Id = id;
        Body = body;
    }

    public int Id { get; set; }

    public T Body { get; set; }
}

Create instance for generic class dynamically:

using System;
using Newtonsoft.Json;

namespace RtxService.Api
{
    public class Program
    {
        public static void Main(string[] args)
        {
                var cat = new Cat()
                {
                    ...
                };

                var type = typeof(Cat);
                var genericType = typeof(Animals<>).MakeGenericType(type);
                var animal = Activator.CreateInstance(
                    genericType,
                    1,
                    JsonConvert.DeserializeObject(cat, type));
        }
    }
}

refs:
https://stackoverflow.com/questions/1151464/how-to-dynamically-create-generic-c-sharp-object-using-reflection

Consume AutoMapper in a Singleton instance during impliment an IHostedService

AutoMapper中的AddAutoMapper默认使用AddScoped方式把AutoMapper实例注册到.NET Core DI中的:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddAutoMapper();
    ...
}

 

如果你在一个IHostedService注册了一个Singleton实例, 该Singleton实例的构造函数中通过DI注入的方式引用了IMapper, 将会发生如下错误:

'Cannot consume scoped service 'AutoMapper.IMapper' from singleton 'XXX'.'

 

解决方法是把IMapper也以Singleton模式注册:

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<SourceClass, DestinationClass>();
    }
}
public void ConfigureServices(IServiceCollection services)
{
    ...
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new AutoMapperProfile());
    });

    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);
    ...
}

 

Refs:

https://stackoverflow.com/questions/40275195/how-to-setup-automapper-in-asp-net-core

What is the difference between services.AddTransient, service.AddScoped and service.AddSingleton methods in ASP.NET Core?


1. Transient objects are always different; a new instance is provided to every controller and every service.
2. Scoped objects are the same within a request, but different across different requests.
3. Singleton objects are the same for every object and every request.

Refs:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2#service-lifetimes-and-registration-options
https://stackoverflow.com/questions/38138100/what-is-the-difference-between-services-addtransient-service-addscoped-and-serv

Configuring /etc/hosts/in Kubernetes Depolyment/Pod

Example of Pod:

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - "/etc/hosts"

Example of Deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: hostaliases-deployment
spec:
  template:
    spec:
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "foo.local"
        - "bar.local"
      - ip: "10.1.2.3"
        hostnames:
        - "foo.remote"
        - "bar.remote"
      containers:
      - name: a-aspnetcore-app
        image: aspnetcore-app:v1.0.0
        env:
        - name: ASPNETCORE_ENVIRONMENT
          value: Development
        ports:
        - containerPort: 80
      imagePullSecrets:
      - name: docker-secret

See the result in Kubernetes container

# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
192.168.240.234 hostaliases-deployment-65d5c48f7c-pqqvn

# Entries added by HostAliases.
127.0.0.1       foo.local bar.local
10.1.2.3        foo.remote bar.remote

reference: add-entries-to-pod-etc-hosts-with-host-aliases

Configuring GitLab external URL

Configuring the external URL for GitLab
In order for GitLab to display correct repository clone links to your users
it needs to know the URL under which it is reached by your users, e.g.
http://gitlab.example.com. Add or edit the following line in
/etc/gitlab/gitlab.rb:

external_url "http://gitlab.example.com"

Run sudo gitlab-ctl reconfigure for the change to take effect.

then restart GitLab: sudo gitlab-ctl restart

 

Refers:

https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/configuration.md#configuring-the-external-url-for-gitlab

https://forum.gitlab.com/t/change-domain-name/1174/2

MongoDB中使用$rename指令修改字段名

如下所示:把collection Policy中的Type为1的所有document的UserID字段修改为UserName, multi为true表明修改所有记录,upsert为false表明记录不存在则不创建:

db.getCollection('Policy').update({"Type":1},{$rename:{'UserID': 'UserName'}},{"multi" : true, "upsert" : false})

参考:https://docs.mongodb.com/manual/reference/operator/update/rename/

Docker Windows容 器中的时间问题

场景 #1:

主机OS版本: Windows 10 1803
容器OS版本: Windows Server Core 1803

容器以默认的 hyperv 模式启动, 空器中的时间是一个莫名其妙的未来时间,比主机的时间提前10多个小时:
主机的时间是2018-8-15 17:XX:XX, 容器中的时间是2018-8-16 07:XX:XX
又一次代码修改重新构建了容器的镜像,重启了容器,容器的时间与主机的时间同步了

测试:
1. 当前实际时间为2018-8-16 16:XX:XX, 关掉主机中的自动设置时间, 修改主机的时间为2018-5-16 16:XX:XX,容器中的时间不变,重启容器后容器中的时间变成2018-8-16 09:XX:XX
2. 打开主机中自动设置时间,主机时间变回,2018-8-16 16:07:XX, 容器的时间也跟着同步成了2018-8-16 16:07:XX
3. 再次关掉主机中的自动设置时间,把主机时间改为2018-8-19 16:07:XX, 容器的时间马上跟着变成了2018-8-19 16:07:XX
4. 再次打开主机中的自动设置时间,主机时间变回2018-8-16 16:09:XX, 容器时间还维持在2018-8-19 16:XX:XX
5. 再次重启容器,容器的时间又与主机同步了

结论: 当容器中的时间比主机的时间晚时,与立即与主机时间同步,反之则不会同步。莫名其妙, 参见bug: https://github.com/moby/moby/issues/37283

场景 #2:

主机OS版本: Windows Server 2016 14393.1358
容器OS版本: Windows Server Core 10.0.14393.2363

容器以 process 模式启动, docker run … –isolation process…
不管主机时间如果变化,容器中的时间都与主机时间同步

hyperv或process兼容列表见:
https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/version-compatibility

Docker for Windows 18.06.0-ce released

18.06.0-ce-win70 (19075)

  • Upgrades
  • New
  • Bug fixes and minor changes
    • AUFS storage driver is deprecated in Docker Desktop and AUFS support will be removed in the next major release. You can continue with AUFS in Docker Desktop 18.06.x, but you will need to reset disk image (in Settings > Reset menu) before updating to the next major update. You can check documentation to save images and backup volumes
    • Fix bug which would cause VM logs to be written to RAM rather than disk in some cases, and the VM to hang.
    • Fix security issue with named pipe connection to docker service.
    • Fix VPNKit memory leak. Fixes docker/for-win#2087, moby/vpnkit#371
    • Fix restart issue when using Windows fast startup on latest 1709 Windows updates. Fixes docker/for-win#1741, docker/for-win#1741
    • DNS name host.docker.internal can be used for host resolution from Windows Containers. Fixes docker/for-win#1976
    • Fix broken link in diagnostics window.
    • Added log rotation for docker-ce logs inside the virtual machine.
    • Changed smb permission to avoid issue when trying to manipulate files with different users in containers. Fixes docker/for-win#2170

License for OS (Windows) inside Docker [reshipment]

How does licensing work?

For production, licensing is at the host level, i.e. each machine or VM which is running Docker. Your Windows licence on the host allows you to run any number of Windows Docker containers on that host. With Windows Server 2016 you get the commercially supported version of Docker included in the licence costs, with support from Microsoft and Docker, Inc.

For development, Docker for Windows runs on Windows 10 and is free, open-source software. Docker for Windows can also run a Linux VM on your machine, so you can use both Linux and Windows containers in development. Like the server version, your Windows 10 licence allows you to run any number of Windows Docker containers.

Windows admins will want a unified platform for managing images and containers. That’s Docker Datacenter which is separately licensed, and will be available for Windows soon.

 

https://blog.docker.com/2017/01/docker-windows-server-image2docker/#h.x2hzndd3qwow